-
March 31st, 2011, 03:46 AM
#1
[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.
-
March 31st, 2011, 03:48 AM
#2
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
-
March 31st, 2011, 03:59 AM
#3
Re: Cannot get string from a txt file
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]?
Victor Nijegorodov
-
March 31st, 2011, 04:01 AM
#4
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 ►►
-
March 31st, 2011, 04:02 AM
#5
Re: Cannot get string from a txt file
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
-
March 31st, 2011, 04:12 AM
#6
Re: Cannot get string from a txt file
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;
◄◄ hypheni ►►
-
March 31st, 2011, 04:30 AM
#7
Re: Cannot get string from a txt file
Thanks a lot
I used the "new" command
The problem solved
-
March 31st, 2011, 04:39 AM
#8
-
March 31st, 2011, 06:58 AM
#9
Re: Cannot get string from a txt file
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
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|