March 3rd, 2013, 05:26 PM
How to read a text file into an array of strings?
In a program, I have a text file (called MyDictionary.txt) which has thousands of words in alphabetical order. I need to make a C program that reads in this text file and then makes an array called
char Words[# of total words in the text file][length of longest word].
How do i do that??
if it helps, this is kind of what the dictionary looks like, except many more words:
if you could give me exactly how to do this that would be great, because I have very little time. Thanks for all the help.
March 4th, 2013, 12:46 AM
Well the simple way is to read the whole file once, just to count the number of words and find the longest.
When you know this, you allocate your array, use the rewind() function to reset the file back to the start, and then read the words into your array.
Last edited by salem; March 4th, 2013 at 01:52 AM.
March 5th, 2013, 04:29 PM
Try this out:
Not completey finished; you still have to sort the values but I think ya good after that. Unless it's not what you want (already works, tested it)
void write() //you can skip this function. Just for writing in the file
FILE *people = fopen("people.txt","w");
fprintf(people,"%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\m%s\n","Allie ","Tommy ","Jacob ","Shelby ","Waldo ","Timmy ","Bob ","Brett ","Mario ");
int size = 0;
FILE *people = fopen("C:\\Users\\mineJons\\Documents\\Visual Studio 2010\\Projects\\hello world1\\hello world1\\people.txt","r");
while(fscanf(people,"%s",peeps[size]) != EOF)