#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    Scotland
    Posts
    185
    Rep Power
    12

    Reading files using Fgets, strange errors on compile.


    Hi all,

    I am trying to write a program to open a file, read the file line by line, and display the contents after the = sign on screen.

    I am having problems, however, here is the code I am using:
    #include <stdio.h>
    #include <strings.h>

    main()

    {
    char word[50];
    char *pntr

    /* Get pointer */
    FILE *input_file_pointer;

    /* Open file */
    input_file_pointer = fopen("clist.cfg","r");

    /* if file is not present then stop */
    if (input_file_pointer == NULL)
    {
    printf("Fail\n");
    return 0;

    }
    else
    /* read in line by line and print on screen what is after the = sign */
    while (fgets(word, 50, input_file_pointer) != EOF)
    {
    pntr = strchr(word, '=');
    pntr++;
    printf("%s", pntr);
    }
    fclose(input_file_pointer);
    }

    when I compile the program I get these errors:

    syntax error before 'FILE'
    new.c:14 'input_file_pointer' undeclared (first use in this function)
    new.c:25 warning:comparison between pointer and integer
    new.c:27 'pntr' undeclared (first use in this function)

    Now I can open the file, and close the file. Somehow adding the while loop with fgets messes things up a little, its prolly quite easy but its starting to bug the hell outta me.

    Any help is greatly appreciated guys.
  2. #2
  3. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    12
    > char *pntr

    You need to add a semi-colon to the end of that line. The clue is here:

    > syntax error before 'FILE'


    > #include <strings.h>

    shouldn't this be <string.h>?
    Last edited by BigBadBob; June 22nd, 2003 at 07:42 AM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    Scotland
    Posts
    185
    Rep Power
    12
    Ok thanks for that, those were silly little things as I thought.

    Reword but still get some problems when compiling, these are two warnings. Anyone care to explain:

    new.c
    new.c(25) : warning C4047: '!=' : 'char *' differs in levels of indirection from
    'const int '
    new.c(32) : warning C4715: 'main' : not all control paths return a value

    oh and I get an error when running this. Think its something to do with when I reach the end of the file.

    using visual studio cl compiler this time for quickness, last time I used cc.

IMN logo majestic logo threadwatch logo seochat tools logo