how do i read a text file's content into an array?
i want it to work like this:
the file contains the word "hello"
the code would read the file and put the text in an array called text. text[0] would be 'h' text[1] would be 'e' and so on....
how do i read a text file's content into an array?
i want it to work like this:
the file contains the word "hello"
the code would read the file and put the text in an array called text. text[0] would be 'h' text[1] would be 'e' and so on....
You'll need to get the file size. Once you have it, then you can create a dynamic array with the capacity of the file size in bytes, then fread() the file into an array.
So you wouldn't just use fgets() then?Originally posted by The Dog
You'll need to get the file size. Once you have it, then you can create a dynamic array with the capacity of the file size in bytes, then fread() the file into an array.
In that FAQ example, stdin is a FILE* to the default input stream (normally the keyboard), but you can use a stream you've fopen'ed yourself.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
can you give an example please?
There's is way to get the file size by seeking from the beginning of file to end of the file, then calling ftell() to get the file pointers position, and voila.
Look up on these functions : fseek(), ftell()
@SMB3Master: Do you want to complete file in memory at once? If you, do you understand how to process it once you have it? I'm kinda guessing you just want to read line by line?
@The Dog: You forgot to mention that using fseek/ftell for determining a file size is not suitable to files opened in text mode.
http://www.eskimo.com/~scs/C-faq/q19.12.html
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
>> You forgot to mention that using fseek/ftell for determining a file size is not suitable to files opened in text mode.
I didn't even realize it. I usually write win32 progs so i use the api functions provided to get file info.
>can you give an example please?
If you choose a fixed-size array with fread (not doing the line-by-line with fgets or using dynamic allocation) you might try something like this.Code:#include <stdio.h> #include <ctype.h> int main(void) { char text[8]; size_t i, count = 0; FILE *file = fopen(__FILE__, "rb"); if ( file != NULL ) { count = fread(text, 1, sizeof text, file); fclose(file); } for ( i = 0; i < count; ++i ) { printf("text[%lu]", (long unsigned)i); printf(isprint(text[i]) ? " = '%c'\n" : " = 0x%02X\n", text[i]); } return(0); } /* my output text[0] = '#' text[1] = 'i' text[2] = 'n' text[3] = 'c' text[4] = 'l' text[5] = 'u' text[6] = 'd' text[7] = 'e' */
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
i want to get the file's contents into an array like i said in my first post.
Your question is still ambiguous, imo.Originally posted by SMB3Master
i want to get the file's contents into an array like i said in my first post.
If you know what's in the file already, what's the point in reading it in the first place? This program produces the same output (near enough) as the previous one:Code:#include <stdio.h> int main(void) { FILE *fp; char text[6]; fp = fopen("myfile.txt", "r"); if (fp) { if (fgets(text, sizeof(text), fp)) { puts(text); } } return(0); }
Code:#include <stdio.h> int main(void) { puts("hello"); return(0); }
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
the purpose is reading a config file, this works perfectly.
thanks
>>how do i read a text file's content into an array?<<
>>the file contains the word "hello"<<
....
>>the purpose is reading a config file<<
Config files consist of more than the word "hello", I'm sure. The Dogs and Daves answers probably won't help you, they were unfortunatley mislead by your rather vague question. Be more specific next time, and you'll be more likely to get a correct answer.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
using hello was only an example.