CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Sep 2009
    Posts
    9

    [RESOLVED] Cannot get string from a txt file

    Hi
    I can't read a line using getline() function.using this code generates a runtime error at "iostream" file :

    This is my code :

    fstream InFile;
    InFile.open("c:\\Colors.txt",ios::in);

    InFile >> TargetCount;

    char *a="12345";
    InFile.getline(a,2); // The error is here

    This is th error :

    Unhandled exception at 0x5f6f3b6c (msvcp100d.dll) in camNew.exe: 0xC0000005: Access violation writing location 0x00b29d74.

  2. #2
    Join Date
    Sep 2009
    Posts
    9

    Re: Cannot get string from a txt file

    If I declare my array like char a[256]; ,it works ... But I want to use char* . How can I do this with char* ?

    Thanks

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Cannot get string from a txt file

    Quote Originally Posted by rostamiani View Post
    Code:
    char *a="12345";
    InFile.getline(a,2); // The error is here
    Your char *a points to the const string "12345" which cannot be changed. Thus you have this Access violation exception.

    Note that char* and char[] are almost synonims, so what is the problem to use char a[256]?
    Victor Nijegorodov

  4. #4
    Join Date
    Jul 2009
    Location
    India
    Posts
    835

    Re: Cannot get string from a txt file

    You are suppose the read string in a. So why initializing a with some value. Make it char a[SOME_ARRAY_BUF]
    ◄◄ hypheni ►►

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Cannot get string from a txt file

    Quote Originally Posted by rostamiani View Post
    If I declare my array like char a[256]; ,it works ... But I want to use char* . How can I do this with char* ?
    The question to you is why do you need to do this with char*?

    Whatever your answer is, it isn't going to be right (just a warning to you before you try to answer the question).

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Jul 2009
    Location
    India
    Posts
    835

    Re: Cannot get string from a txt file

    Quote Originally Posted by rostamiani View Post
    If I declare my array like char a[256]; ,it works ... But I want to use char* . How can I do this with char* ?

    Thanks
    by declaring char* you are telling that its a pointer which doesn't allocate a memory space.. so you need to allocate it then.. so use..

    Code:
    char *a;
    a = new char [256];
    
    ...
    ...
    
    delete []a;
    ◄◄ hypheni ►►

  7. #7
    Join Date
    Sep 2009
    Posts
    9

    Re: Cannot get string from a txt file

    Thanks a lot
    I used the "new" command

    The problem solved

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: [RESOLVED] Cannot get string from a txt file

    Why new?
    Why not create an array on the stack?
    Well, if you if you do want use char* and only char* then just do:
    Code:
    char b[256]; 
    char* a = b;
    InFile.getline(a,2);
    No new/delete is needed.

    PS: and why at all do you still use plain char arrays in C++? How about std::string?
    Victor Nijegorodov

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Cannot get string from a txt file

    Quote Originally Posted by rostamiani View Post
    Thanks a lot
    I used the "new" command

    The problem solved
    The problem is not "solved". First, who deletes the memory?

    Second, and most important, by you using new/delete means that you do not really know what you did, and you need to understand things about pointers, addresses, and function parameters. This is the reason why I asked the question to you (which you still didn't answer).

    You thought that just because a function has a parameter of char*, you must declare a char* and somehow use it. WRONG!

    When a parameter for a function requires a pointer, all it needs is an address of that type. You do not need to declare a pointer to get the address of a variable.
    Code:
    void func(char *ptr)
    {
    }
    
    int main()
    {
       char abc[100];
       func (abc);
    }
    Just because the parameter that func() needs is a char*, all you need is to pass the address of a char. The name of the array is the same as the address of the first element of the array, which is a char*.

    If you don't understand this simple concept, then in the future, you will be doing absolutely ridiculous things like the code below:
    Code:
    void func (float *pFloat)
    {
       *pFloat = 10.0F;
    }
    
    int main()
    {
       float *p = new float
       func( p );
    }
    The main() function is nonsense, but a programmer who doesn't understand pointers, arguments, and addresses will write it this way. The way it should have been written is this:
    Code:
    int main()
    {
       float p;
       func( &p );
    }
    Understand now? There is no need to declare a pointer just because a function requires a pointer.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; March 31st, 2011 at 07:06 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured