Thread: Whats wrong with this code?

  1. #1
    Registered User
    Join Date
    Aug 2019
    Posts
    4

    Whats wrong with this code?

    Code:
    int importFile(const char* filename, double content[], int len)
    {
        FILE* fp = fopen(filename, "r");
        double input;
        int i;
    
    
        if(fp == NULL)
        {
            printf("ERROR");
            fclose(fp);
            return -1;
        }
    
    
        for(i = 0; i < len; i++)
        {
            fgets(content, len, fpin);
            if (sscanf(content, "%lf", &input) == 1)
            {
                content[i] = input;
    
    
            }
        }
    
    
        fclose(fp);
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    It has many compilation errors.
    Code:
    foo.c: In function ‘importFile’:
    foo.c:19:29: error: ‘fpin’ undeclared (first use in this function)
             fgets(content, len, fpin);
                                 ^
    foo.c:19:29: note: each undeclared identifier is reported only once for each function it appears in
    foo.c:19:15: warning: passing argument 1 of ‘fgets’ from incompatible pointer type [-Wincompatible-pointer-types]
             fgets(content, len, fpin);
                   ^
    In file included from foo.c:1:0:
    /usr/include/stdio.h:622:14: note: expected ‘char * restrict’ but argument is of type ‘double *’
     extern char *fgets (char *__restrict __s, int __n, FILE *__restrict __stream)
                  ^
    foo.c:20:20: warning: passing argument 1 of ‘sscanf’ from incompatible pointer type [-Wincompatible-pointer-types]
             if (sscanf(content, "%lf", &input) == 1)
                        ^
    In file included from /usr/include/features.h:367:0,
                     from /usr/include/stdio.h:27,
                     from foo.c:1:
    /usr/include/stdio.h:448:12: note: expected ‘const char * restrict’ but argument is of type ‘double *’
     extern int __REDIRECT_NTH (sscanf, (const char *__restrict __s,
                ^
    You're trying to use content as your input buffer for chars, and your output buffer for converted data.
    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.

  3. #3
    Registered User
    Join Date
    Aug 2019
    Posts
    4
    i changed it to this but it still doesn't work and i don't get any error messages

    Code:
    int importFile(const char* filename, double content[], int len)
    {
        FILE* fp = fopen(filename, "r");
        double input;
        int i;
    
    
        if(fp == NULL)
    	{
    		printf("ERROR");
    		fclose(fp);
    		return -1;
    	}
    	else
        {
            for(i = 0; i < len; i++)
            {
               if(fscanf(fp, "%lf", content, &input))
                {
                    content[i] = input;
    
    
                }
            }
    
    
        }
    
    
        fclose(fp);
        return 0;
    }

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5

  6. #6
    Old Fashioned
    Join Date
    Nov 2016
    Posts
    137
    First thing I see wrong is that you have the following code:

    Code:
        if(fp == NULL)
        {
            printf("ERROR");
            fclose(fp);
            return -1;
        }
    This code is incorrect because if fp == NULL, you DO NOT want to call fclose(fp) because that would mean you are trying to close a file handle which isn't even open to begin with... You're trying to close a NULL file handle... Undefined behavior.

    Second issue is that you are improperly using fscanf as pointed out above. You are specifying two areas of memory that you want fscanf() to read bytes into but you're only providing one format string. It's like doing this:

    Code:
    printf("My name is: %s\n", name, some_int);
    That usage is incorrect because there is no format specifier for some_int here and in your case, you have no format specifier for the content variable. It just seems you are totally misunderstanding and misusing fscanf() here. What are you trying to accomplish? Are you trying to read a bunch of doubles from a file into a buffer in memory? If so, this aint the way to do it. You could use fread() to do that.
    Last edited by Asymptotic; 09-09-2019 at 12:22 AM.
    If I was homeless and jobless, I would take my laptop to a wifi source and write C for fun all day. It's the same thing I enjoy now!

  7. #7
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Turn ON your error & warning feature in your compiler, and TURN UP the warning level to the highest level!!!

    I get one error even after adding:

    Code:
    #include <stdio.h>

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. whats wrong with this code.
    By abhisheksahni in forum C Programming
    Replies: 7
    Last Post: 03-06-2013, 12:27 PM
  2. whats wrong with my code
    By shyam.sunder91 in forum C Programming
    Replies: 3
    Last Post: 03-12-2012, 06:27 AM
  3. Whats wrong with this code?!
    By Death_Wraith in forum C++ Programming
    Replies: 4
    Last Post: 03-12-2004, 04:18 PM
  4. whats wrong with this code??
    By stehigs321 in forum C Programming
    Replies: 2
    Last Post: 10-29-2003, 11:00 PM
  5. whats wrong with this code i have?
    By stehigs321 in forum Game Programming
    Replies: 1
    Last Post: 10-29-2003, 08:38 PM

Tags for this Thread