cc2006
asked on
Reading text file items to an array
Hi,
I'm trying to read some strings in from a text file, and I'm having so many problems with it. Urgh it's annoying when it takes days to do something that should be simple. All I really want to be able to do, is read in all items and put it in an array. item1 in array[0], item2 in array[1] etc.
The program is ran by calling the program and specifying the text file. So you run this: test items.txt
I have a textfile called 'items.txt', inside is:
item1
item2
item3
item4
I've now got:
---
int main(int argc, char **argv)
{
FILE *ifp;
char items[20];
int i = 0;
ifp = fopen(argv[1], "r");
if (ifp != 0) {
while (!feof(ifp)) {
???
}
}
}
}
Can anyone please help me.
I'm trying to read some strings in from a text file, and I'm having so many problems with it. Urgh it's annoying when it takes days to do something that should be simple. All I really want to be able to do, is read in all items and put it in an array. item1 in array[0], item2 in array[1] etc.
The program is ran by calling the program and specifying the text file. So you run this: test items.txt
I have a textfile called 'items.txt', inside is:
item1
item2
item3
item4
I've now got:
---
int main(int argc, char **argv)
{
FILE *ifp;
char items[20];
int i = 0;
ifp = fopen(argv[1], "r");
if (ifp != 0) {
while (!feof(ifp)) {
???
}
}
}
}
Can anyone please help me.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> Oh right, I thought char items [10][20]; was only used on 2d arrays.
This is a 2D array of characters !
Remember, a string is an array of characters. And you want an array of strings. So, you get an array of an array of characters, or a 2D array of characters.
>> Thanks, I'll play around with that.
Feel free to post code or ask additional questions !
This is a 2D array of characters !
Remember, a string is an array of characters. And you want an array of strings. So, you get an array of an array of characters, or a 2D array of characters.
>> Thanks, I'll play around with that.
Feel free to post code or ask additional questions !
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> items[i] = malloc(strlen(line));
>> strcpy( items[i], line );
Make this :
items[i] = malloc(strlen(line) + 1);
strcpy( items[i], line );
Or even better, use strncpy() instead of strcpy().
>> strcpy( items[i], line );
Make this :
items[i] = malloc(strlen(line) + 1);
strcpy( items[i], line );
Or even better, use strncpy() instead of strcpy().
ASKER
Thanks guys I'll have a look at that.
What's the main difference between strncpy() instead of strcpy() then?
What's the main difference between strncpy() instead of strcpy() then?
>> What's the main difference between strncpy() instead of strcpy() then?
strncpy() is safer because it allows you to specify a length, which, if done correctly, avoids overflows.
http://www.cplusplus.com/ref/cstring/strcpy.html
http://www.cplusplus.com/ref/cstring/strncpy.html
strncpy() is safer because it allows you to specify a length, which, if done correctly, avoids overflows.
http://www.cplusplus.com/ref/cstring/strcpy.html
http://www.cplusplus.com/ref/cstring/strncpy.html
Thanks infinity! (it's all coming back to me as if in a dream)
ASKER
Thanks, I'll play around with that.