fgets help!!

This is a discussion on fgets help!! within the C Programming forums, part of the General Programming Boards category; yes, im a newb. now that its out of the way, i need a little help with the fgets() and ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    1

    fgets help!!

    yes, im a newb. now that its out of the way, i need a little help with the fgets() and fscanf() functions ...

    Code:
    while (fgets(tvshow, NAMESIZE, inFile) != NULL)
    	{
    		
    		fscanf(inFile, "%s", &tvshow);
    		printf("\n%s", tvshow);
    
    	}
    basically im trying to create a loop where a text file is opened and each line is read and then displayed on the screen. the file and everything works fine but for some reason it only displays the first word of each name. for example "The Simple Life" will only appear as "The". I think i might have to put a statement in that allows it to keep reading the line when it reaches a blank space, i just dont know how to do it. any help is appreciated. thanks

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    181
    well, if you want to read the data and just print it on screen why dont u do this
    Code:
    #include<stdio.h>
    
    int main()
    {
        FILE *fp;
        char str[80];
        
        if((fp=fopen("text.txt","r"))==NULL)
        {
            printf("Error: Opening file");
            exit(1);
        }
        
        while(fgets(str,80,fp)!=NULL)
            fputs(str,stdout);
        
        getchar();
    }
    and my text is
    Code:
    this is a test message
    this is how fgets read the data
    and prints it on the screen
    -s.s.harish

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You've already read the line, when you call fgets in your loop statement. So why are you trying to read again with fscanf? Consider reading the FAQ entries on getting a line.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    . . . it only displays the first word of each name. For example "The Simple Life" will only appear as "The".
    That's what scanf does. scanf(), fscanf(), or sscanf() . . . it's all the same. I don't know how to fix it, but you can, I've seen someone (Salem I think) do it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets not working after fgetc
    By 1978Corvette in forum C Programming
    Replies: 3
    Last Post: 01-22-2006, 05:33 PM
  2. problem with fgets
    By learninC in forum C Programming
    Replies: 3
    Last Post: 05-19-2005, 08:10 AM
  3. problem with fgets
    By Smoot in forum C Programming
    Replies: 4
    Last Post: 12-07-2003, 02:35 AM
  4. fgets crashing my program
    By EvBladeRunnervE in forum C++ Programming
    Replies: 7
    Last Post: 08-11-2003, 12:08 PM
  5. help with fgets
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-17-2001, 08:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21