problems with opening a file from a string

This is a discussion on problems with opening a file from a string within the C Programming forums, part of the General Programming Boards category; I am using linux and i'm haveing a problem with this code. It doesn't even seem to open the file. ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    21

    problems with opening a file from a string

    I am using linux and i'm haveing a problem with this code. It doesn't even seem to open the file. I also tried useing the gets function but GCC warns me that gets() is dangerous and should not be used.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #define MAX_CHAR 40
    #define MAX_ARRAY 500
    int main(void)
    {
            FILE *inp, *outp;
            char instring[MAX_CHAR], outstring[MAX_CHAR];
            double in_array[MAX_ARRAY];
            double current_status;
            int i=0;
            int num;
            printf("please enter a file you would like to use.");
            scanf("%s", instring);
            printf("please enter a file you would like to ouput to");
            scanf("%s", outstring);
    
            inp = fopen(instring, "r");
            outp = fopen(outstring, "w");
            //read the text file
            while(fscanf(inp, "%lf", current_status) != EOF)
            {
                    num += 1;
                    in_array[i] = current_status;
            }
            printf("I read %i numbers", num);
            //print the old data to the new text file
            for(i = 0; i<num; i++)
            {
                    fprintf(inp, "%f", in_array[i]);
            }
            fclose(inp);
            fclose(outp);
    
            return 0;
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    fopen() returns NULL if it failed to open the file so you should check if inp or outp are NULL after you try opening those and before you actually try using them.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    You should get in the habit of testing whether or not the fopen was a success:

    Code:
    inp = fopen(instring, "r");
    outp = fopen(outstring, "w");
    
    if( inp == NULL || outp == NULL )
    {
        printf("Error: Could not open one or more required files.");
        return 0;
    }
    
    //Missing the & below
    while(fscanf(inp, "%lf", &current_status) != EOF)
    {
        num += 1;
        // You need to be changing the value of "i" here or else you are
        // simply overwriting the same array position over and over
        in_array[i] = current_status;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    21
    Dude, you guys rock. I still can't believe i made that small error with the &. Now i have one problem it opens, it reads, but the printing does not work.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <stdlib.h>
    #define MAX_CHAR 40
    #define MAX_ARRAY 500
    int main(void)
    {
            FILE *inp, *outp;
            char instring[MAX_CHAR], outstring[MAX_CHAR];
            double in_array[MAX_ARRAY];
            double current_status;
            int i=0;
            int num;
            printf("please enter a file you would like to use.");
            scanf("%s", instring);
            printf("please enter a file you would like to ouput to");
            scanf("%s", outstring);
    
            inp = fopen(instring, "r");
            outp = fopen(outstring, "w");
            if( inp == NULL || outp == NULL )
            {
                        printf("Error: Could not open one or more required files.");
                            return 0;
            }
            //read the text file
            while(fscanf(inp, "%lf",&current_status) != EOF)
            {
                    num += 1;
                    in_array[num -1] = current_status;
    
            }
            printf("I read %i numbers", num);
            //print the old data to the new text file
            for(i = 0; i<num; i++)
            {
                    //there hase to be a problem with this printf statement. it doesn't print to the file.
                    fprintf(inp, "%f", in_array[i]);
             //added this to see if it really was print the correct number and it is.      
             printf("current number being printed is %f", in_array[i]);
            }
            printf("I printed %i numbers", i);
            fclose(inp);
            fclose(outp);
    }

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    21
    oh my. today is not my dad. i printed to the input file.

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    21

    terms in c

    in my c class the professor uses the terms call by reference and call by value. I can not find these in the book. could anyone explain this to me?

  7. #7
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM

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