![]() |
| ||
| Read in a string from a text file Need help with a function that reads from a text file usins fscanf function. example say there is 20 stings that are 60 chars long on their own individual line. Does fscanf() read the whole text file or does it read only up to the carriage return. If it doesn't how can you only read the first string in the text file up to the carriage return using fscanf(), i am using C code. output of text file hellothereadkdkdkdkd aldldjfjbualdfljdlfjdfjdl aldfl;jlfdljflfldjfldjbyhh my program does not allow to have spaces in between the 60char string, so i cannot figure how to read the first string in the text because there is a carriage return after every line so it does not look like one massive string. i know that the type File pointer , points to the beginning of the text file but how do you procedd to read in the first string only????? Please help!! |
| ||
| Re: Read in a string from a text file Quote:
Quote:
|
| ||
| Re: Read in a string from a text file >>string[strlen(string)] = '\0'; That may or may not work -- if the string does not contain '\n' the above will cut off the last character that was read. fgets() appends the '\n' ONLY if there is one in the stream -- for example if the buffer is filled up before reaching end-of-line or when the last line in the file does not contain '\n'. So the safest way to do it is to check if the last character is '\n' int len = strlen(string) -1; |
| All times are GMT -4. The time now is 5:23 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC