Thread: fgets in C Programming

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    12

    fgets in C Programming

    I am relatively new to C Programming and have been working for about a week to try and make the following code work.

    I have been able to open the file, but when I try to read a line with fgets, I get nothing and a parse error prior to char *num. When I remove the tokenString. . . I compile with no error, but of course, it doesn't read the line. Would someone check the syntax of what I've written and see what I'm missing?



    Ptrbook=fopen("Book.dat","r");

    if(Ptrbook == NULL) printf("Error opening file\n");

    else (printf("File is open\n"));

    { /*Open book assign*/

    tokenString = (fgets(buffer,int RECORDLENGTH, Ptrbook));

    char *num = strtok(oneline, "\t");
    char *name = strtok(NULL, "\t");
    char *author = strtok(NULL, "\t");
    char *pub = strtok(NULL, "\n");
    int i;

    printf("%d\n", num);
    printf("%s\n", name);
    printf("%s\n", author);
    printf("%s\n", pub);

    fclose(Ptrbook);


    Thank you! Your assistance would be greatly appreciated!

    Readerwhiz

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    int RECORDLENGTH

    whats the int doing there??
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    This is how you would print the text file to stdout

    Code:
    char buff[100];
    while ( fgets( buff, sizeof(buff), Ptrbook ) != NULL ) {
        printf( "%s", buff );
    }
    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.

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, 06: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, 03: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