[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.
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
Re: Cannot get string from a txt file
Quote:
Originally Posted by
rostamiani
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]? :confused:
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]
Re: Cannot get string from a txt file
Quote:
Originally Posted by
rostamiani
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
Re: Cannot get string from a txt file
Quote:
Originally Posted by
rostamiani
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;
Re: Cannot get string from a txt file
Thanks a lot :)
I used the "new" command
The problem solved
Re: [RESOLVED] Cannot get string from a txt file
Why new? :confused:
Why not create an array on the stack? :confused:
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?
Re: Cannot get string from a txt file
Quote:
Originally Posted by
rostamiani
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