Thread: Need HELP !!! Errors in my fgets function

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    31

    Need HELP !!! Errors in my fgets function

    Here is the code I have so far, thanks in advance.

    Code:
     #include "area.h"
    
    /* this function reads the X- and Y-coordinates of points from a file
     * creates a doubly linked circular list of the points
     * returns a pointer to the list
     */
    node *readPolygon(char filename[])
    {
      node *polygon;
    
    
    
    
      /* use fopen() to open the file
       * make sure the file is opened -- i.e. fopen() is successful
       * exit() the program if not successful
       * initialize an empty linked list
       *
       * loop
       *   use fgets() to read one line from the file
       *   use sscanf() to get the X- and Y-coordinates of a point
       *   use insertTail() to insert the point to the tail of the list
       *
       * don't forget to close the file by fclose()
       */
    
    
      FILE *filepointer;
    
    
      char record  [1000];
    
    
      node *linkedlist;
    
    
      double x, y;
      int first = 0;
    
    
      filepointer = fopen(filename, "r");
    
    
      if (filepointer == NULL){
        printf("Error opening file\n");
        exit(-1);
      }
    
    
      fgets (record, filepointer);
    
    
      while (char !=NULL){
    
    
        sscanf(filepointer, "%lf, %lf," &x, &y);
        polygon = insertTail(polygon, x,y);
    
    
        /*    if (first ==0){
          initList(node);
        }
        else {
          insertTail(list, x, y);
        }
    
    
        fgets (record, filepointer);*/
    
    
      }
    
    
       fclose(filepointer);
    
    
       return polygon;
    }
    
    
    
    }
    Code:
    
    
    icc -ansi -Wall -c -o readPolygon.o readPolygon.c
    
    Error that im getting mostly in my fgets function
    
    readPolygon.c(53): warning #810: conversion from "FILE *" to "int" may lose significant bits
        fgets (record, filepointer);
                       ^
    
    
    readPolygon.c(53): error #165: too few arguments in function call
        fgets (record, filepointer);
                                  ^
    
    
    readPolygon.c(55): error: expected an expression
        while (char !=NULL){
               ^
    
    
    readPolygon.c(57): error: expression must have integral type
          sscanf(filepointer, "%lf, %lf," &x, &y);
                              ^
    
    
    readPolygon.c(57): error: expression must have integral type
          sscanf(filepointer, "%lf, %lf," &x, &y);
                                           ^
    
    
    readPolygon.c(57): warning #167: argument of type "FILE *" is incompatible with parameter of type "const char *__restrict__"
          sscanf(filepointer, "%lf, %lf," &x, &y);
                 ^
    
    
    readPolygon.c(58): error: a value of type "void" cannot be assigned to an entity of type "node *"
          polygon = insertTail(polygon, x,y);
                  ^
    
    
    compilation aborted for readPolygon.c (code 2)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Some of these error messages should be rather self-explanatory. What have you tried in order to fix them?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2018
    Posts
    21
    Code:
    yours
    readPolygon.c(53): error #165: too few arguments in function call
        fgets (record, filepointer);
    
    the definition for fgets
    
    char *fgets(char *str, int n, FILE *stream)
    what pram are you missing? I'd suggest looking up the function call for an example on how to use it, and correct format specifier for double in sscanf, your int data type specifier too , how to use sscanf over all, , and functions on return values, when to use them, and while loops in how to use them.what would your condition actually be that the loop can read and keep checking? ie not a data type. last but not least, fscanf vs sscanf
    Last edited by poorboy; 11-28-2018 at 07:04 PM.

  4. #4
    Registered User
    Join Date
    Sep 2018
    Posts
    31
    Quote Originally Posted by laserlight View Post
    Some of these error messages should be rather self-explanatory. What have you tried in order to fix them?
    Code:
    readPolygon.c(53): warning #810: conversion from "FILE *" to "int" may lose significant bits
        fgets (record, filepointer);
    
    
    readPolygon.c(53): error #165: too few arguments in function call
        fgets (record, filepointer);
    
    fixed:
     fgets (str, sizeof(str), filepointer);
    Left with these errors,im trying any recommendations

    Code:
    readPolygon.c(55): error: identifier "record" is undefined    while (record !=0){
               ^
    
    
    readPolygon.c(57): error: expression must have integral type
          sscanf(filepointer, "%lf, %lf," &x, &y);
                              ^
    
    
    readPolygon.c(57): error: expression must have integral type
          sscanf(filepointer, "%lf, %lf," &x, &y);
                                           ^
    
    
    readPolygon.c(57): warning #167: argument of type "FILE *" is incompatible with parameter of type "const char *__restrict__"
          sscanf(filepointer, "%lf, %lf," &x, &y);
                 ^
    
    
    readPolygon.c(58): error: a value of type "void" cannot be assigned to an entity of type "node *"
          polygon = insertTail(polygon, x,y);
                  ^
    
    
    compilation aborted for readPolygon.c (code 2)

  5. #5
    Registered User
    Join Date
    Nov 2018
    Posts
    21
    Quote Originally Posted by erald23 View Post

    Left with these errors,im trying any recommendations

    Code:
    readPolygon.c(55): error: identifier "record" is undefined    while (record !=0){
               ^
    
    
    readPolygon.c(57): error: expression must have integral type
          sscanf(filepointer, "%lf, %lf," &x, &y);
                              ^
    
    
    readPolygon.c(57): error: expression must have integral type
          sscanf(filepointer, "%lf, %lf," &x, &y);
                                           ^
    
    
    readPolygon.c(57): warning #167: argument of type "FILE *" is incompatible with parameter of type "const char *__restrict__"
          sscanf(filepointer, "%lf, %lf," &x, &y);
                 ^
    
    
    readPolygon.c(58): error: a value of type "void" cannot be assigned to an entity of type "node *"
          polygon = insertTail(polygon, x,y);
                  ^
    
    
    compilation aborted for readPolygon.c (code 2)
    go back up and read my post, every error is what I suggest you look up, into . plus maybe look into how to use fscan in conjunction with sscan when using a loop checking for eof. other wise figure out what fscan is used for, and what is sscan used for, specificity.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets and readRestOfLine errors
    By iamamagicman in forum C Programming
    Replies: 1
    Last Post: 08-26-2015, 09:21 AM
  2. Replies: 3
    Last Post: 04-19-2013, 12:59 PM
  3. Fgets and Fprintf Errors?
    By Charak in forum C Programming
    Replies: 9
    Last Post: 03-23-2011, 09:06 AM
  4. Problem in fgets(); function ?
    By lionaneesh in forum C Programming
    Replies: 2
    Last Post: 04-01-2010, 12:22 PM
  5. fgets in a function
    By Granger9 in forum C Programming
    Replies: 3
    Last Post: 09-12-2002, 02:50 PM

Tags for this Thread