Could someone tell me which function shoud I use to read text from any txt file?
With fscanf() I read data with specific format. I don't want that...I want to read just any text and output some strings in a specific way.
Could someone tell me which function shoud I use to read text from any txt file?
With fscanf() I read data with specific format. I don't want that...I want to read just any text and output some strings in a specific way.
Typically,
If you have unusually long lines, and this is important information, then you can easily detect whether fgets() has returned a 'short' line, and you can adjust accordingly.Code:char buff[BUFSIZ]; while ( fgets( buff, sizeof buff, fp ) != NULL ) { // do stuff with a line in buff, which typically ends with \n\0 // and always ends in \0 }
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
In my program I want to read text from a file and inverse those words that are equal to a word given. I don't know the length of each line, so I can't assign a BUFSIZ which will always be enough.
For example, if I read 1000 characters each time, how can I deal with the situation in which a word exists between line[998] and line[1002]? The word will be separated into two pieces and won't be checked correctly for equality with the word given.
Last edited by Cevris; 05-12-2011 at 06:50 AM.
First what are you trying to do?
'I want to read just any text' is not very helpful for us.With fscanf() I read data with specific format. I don't want that...I want to read just any text and output some strings in a specific way.
You want to read entire line? or what?
I have a .txt file which includes text. It can have one line, or two or ten. Maybe it's only one word. In any case I want to check if any of the word(s) in the text are equal to a specific word given to me. I thought that I could read a line or 1000 chars each time using fgets() and the splitting the string into words using strtok(). But with that possible solution, I need to find a way to deal with the situation which I describe in my previous post.
fgets reads text up to the size of the buffer provided, or until it hits a newline character. If it fills the buffer, there will be no newline in the buffer. At that point, you can allocate memory for a secondary buffer to hold what you have + some extra amount, copy what you have into the new buffer and read again into the original buffer. Repeat as necessary. When you've hit the newline, copy the final data into the secondary buffer and use it.
words?
Then use fscanf()?
Or write your own function if you're not sure what'll be maximum length of word.
eg.
Code:while( fscanf(fp,"%100s",word) != EOF ) { if( strcmp(word, ..... ) == 0 ) { } }
If there is room in the buffer for the \n, then there will be a \n present, and it will be the last character in the buffer.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.