Thread: File input with fgets

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    21

    File input with fgets

    Dealing with files seems pretty awkward and complicated to me and also inconsistent in the results.

    With the following code:

    #include <stdio.h>
    #include <string.h>


    Code:
    int main()
    {
    
    	FILE *fp;
    	
    	char string[40];
    	int length;
    	length= strlen(string);  
    
    	fp = fopen("teamresults.txt", "r");
    
    	fgets(string,40,fp);   
    	
    	printf("%d %s \n",length,string);
    
    	fclose (fp);
    }
    I get different results in the length of the string in my text file. I have gotten 26, 44 and 73. The text file is merely "ManchesterUtd01Reading", not in quotes though, which is 22 characters.

    Apparently with fgets(), it reads a specified number of characters from the file, or until a new line is reached, or until end of file. Well, I asked for 40 but there were only 22 characters, so why did it not stop at 22 and count 22?

    Ultimately what I am trying to do is create a league table, and I am just playing around at the moment. The way I see it is that I'll have to count how many characters there are on the first line, store this and use it in my loop to loop through the characters 1 by 1 until a number is reached, store that string in array as team 1, then loop again looking for numbers and store this in a score array, then loop again from after the score to read in the second team to another array. I don't know what after that because at the moment I can't even count the correct number of characters on the first line.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't find the length of the string before you read it in, you silly person. Try moving the strlen call to after the fgets call.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    7
    Code:
    int main()
    {
    
    	FILE *fp;
    	
    	char string[40];
    	int length;
    	
    
    	fp = fopen("teamresults.txt", "r");
    
    	fgets(string,40,fp);   
    	length= strlen(string);  
    	printf("%d %s \n",length,string);
    
    	fclose (fp);
    }

    now it will work fine

  4. #4
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Quote Originally Posted by bharat_kaushik View Post
    [code]
    now it will work fine
    Now it has to work fine.......... Why are you still in a confusion abt it?

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    21
    lol oops

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  3. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  4. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 04:49 PM
  5. Replies: 6
    Last Post: 01-03-2007, 03:02 PM