Thread: help w/ file I/O

  1. #1
    Unregistered
    Guest

    help w/ file I/O

    i'm trying to open a read a file that i wrote in notepad...it's a series of tables...four columns w/ five numbers in each...it should be rather simple but i can't even get it to read anything....even the first 5 floating values...let alone the whole database...and exit(1) will not work in a VMS system...but i can't figure out what else will....if anyone has any insight it's much appreciated...
    #include <string.h>
    void main (void)
    {
    FILE *in_file;
    int i;
    float val, n;
    in_file = fopen("program.txt","r"); /*trying to open file*/

    if (in_file == (FILE *) NULL)
    {
    printf("\nFailed to open the data file.\n");
    exit(1);
    }
    printf("\nThe file has been successfully opened for reading.\n");

    for (i = 1; i <= 5; i++)
    {fscanf(in_file,"%0.2f %f", &n, &val);
    printf("\n%0.2f %f", n, val);}
    fclose(in_file);
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    375
    exit() only exists if you #include <stdlib.h> .. it is a standard function so I would imagine it would work. If it doesn't you could create a label at the end of your program and use a goto I suppose.

    fscanf() is.. touchy. You might try reading in one variable at a time instead of two. That could help with things like spacing issues. Is it reading anything? I would think that opening a file "r"ead style starts your file pointer off at the beginning, but you might try putting a rewind() function in there. Also, if it is failing on the opening of the file, you might throw a path in the fopen function - ie fopen("C:\\Directory\\program.txt", "r");

    I hope that is in some way helpful.
    Allegro precompiled Installer for Dev-C++, MSVC, and Borland: http://galileo.spaceports.com/~springs/

  3. #3
    Unregistered
    Guest
    i too your advice...and the exit thing works now...but it's still not reading the file whatsoever...but i wouldn't know that unless u helped me w/ that exit thing...thank you very much...i'll keep trying to get it to open....if u have anymore ideas then those will be much appreciated also...
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    void main (void)
    {
    FILE *in_file;
    int i;
    float val, n;
    in_file = fopen("C:\\Directory\\program.txt", "r"); /*trying to open file*/

    if (in_file == (FILE *) NULL)
    {
    printf("\nFailed to open the data file.\n");
    exit(1);
    }
    printf("\nThe file has been successfully opened for reading.\n");

    for (i = 1; i <= 5; i++)
    {fscanf(in_file,"%0.2f", &n);
    printf("\n%0.2f", n);}
    fclose(in_file);
    }

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    2

    hi, i registered...so now i won't be such a stranger

    i registered for this board now...i got tired of seeing "Unregistered User" instead of my user name...though, my program still won't work...i've tinkered w/ it but have had no progress whatsoever w/ it....maybe i'm getting the pathway wrong...it stills says it's not open

  5. #5
    Unregistered
    Guest
    I would change the part where you open the file to this:

    if ((in_file = fopen(file_name, "r")) == NULL) {
    fprintf(stderr, "***> Open error reading input file %s", file_name);
    exit(-1);
    } /* end if */


    And as far as reading the file try doing something like this:
    (I think you should be reading strings and not floating)

    fgets(string_variable, 81, in_file);

    while(! feof(in_file)) {
    printf("%s", string_variable);
    fgets(string_variable, 81, in_file);
    } /* end while */

    fclose(in_file);


    The function fgets() breaks down like this:
    fgets(string_variable, max_length_of_string, fileName);

    Tell me if it works.

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    2

    can't get it to work...:(

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    void main (void)
    {
    FILE *in_file;
    int i;
    float val,n;
    char string_variable[81];
    in_file = fopen("file:///C:/program.txt", "r"); /*trying to open file*/

    if ((in_file = fopen(in_file, "r")) == NULL) {
    fprintf(stderr, "***> Open error reading input file %s", in_file);
    exit(-1);
    } /* end if */


    printf("\nThe file has been successfully opened for reading.\n");



    fgets(string_variable, 81, in_file);

    while(! feof(in_file)) {
    printf("%s", string_variable);
    fgets(string_variable, 81, in_file);
    } /* end while */

    fclose(in_file);
    }

    i might've not completely understood what you were doing in that code but i got the basic idea i believe...but it's giving me errors so i don't know if it works or not...maybe i just did it wrong or something

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    #include <stdio.h>
    
    int main ( ) {
        char buff[BUFSIZ];
        FILE *fp;
        fp = fopen( "file.txt", "r" );
        while ( fgets(buff,BUFSIZ,fp) != NULL ) {
            int res;
            float val,n;
            res = sscanf( buff, "%f %f", &val, &n );
            if ( res == 2 ) {
                printf( "Success! - %f %f\n", val, n );
            } else {
                printf( "Garbage in line %s", buff );
            }
        }
        fclose( fp );
        return 0;
    }

  8. #8
    Registered User
    Join Date
    Nov 2001
    Posts
    8
    in_file = fopen("file:///C:/program.txt", "r"); /*trying to open file*/<======= This line is not needed.

    Since it opens the file in the if statement below. Look at the if statement....there should be the exact filename where you open it up(if you open it up in the same folder as the program you just need the filename). I changed it so you can see the correct way.

    if ((in_file = fopen("program.txt", "r")) == NULL) {
    fprintf(stderr, "***> Open error reading input file %s", in_file);
    exit(-1);
    } /* end if */

  9. #9
    Unregistered
    Guest

    i hate to be a bother

    but i didn't get that program to work w/ all of the tinkering that i had to do w/ it....it turns out that since i was doing that program in a VMS system that there was no way it would work at all....so, that sux...so, i had to delete all of the code that i was working on that was VMS specific and i started over w/ Microsoft Visual C++...and it works....i just need some adivce on how to make the output look like this....u don't have to do it for me...at the very least just help me w/ the partial code needed to do this and i can figure out the rest

    needs to look like this:

    Quantity On Reorder Minimum
    Part No. Price Hand Point Order
    0123 1.23 23 20 20
    0234 2.34 34 50 25
    3456 34.56 56 10 10
    5678 6.78 7 75 25

    any help at all will be much appreciated...as i've appreciated all of the help from the ppl on this board..thank you

  10. #10
    Registered User breed's Avatar
    Join Date
    Oct 2001
    Posts
    91
    just use the printf()
    then display each format specifier with the number of digits required// printf(a four digit integer would look like "%4d
    a three digit float with a 2 decimal point would look like %4.2f
    and an array that holds 7 char's looks like %7s", num, currency, name);
    Before you judge a man, walk a mile in his
    shoes. After that, who cares.. He's a mile away and you've got
    his shoes.
    ************William Connoly

  11. #11
    Unregistered
    Guest
    /* Reading and printing a sequential file */
    #include <stdio.h>

    main()
    {
    int account;
    float name;
    int balance;
    FILE *cfPtr; /* cfPtr = clients.dat file pointer */

    if ((cfPtr = fopen("N:///CST/1110-1/TURNIN/s_virick/labtext.dat", "r")) == NULL)
    printf("File could not be opened\n");
    else {
    printf("%-10s%-13s%s\n", "Part No.", "Price", "Quantity");
    fscanf(cfPtr, "%d%f%d", &account, &name, &balance);

    while (!feof(cfPtr)) {
    printf("%-10d%-13f%7.2d\n", account, name, balance);
    fscanf(cfPtr, "%d%f%d", &account, &name, &balance);
    }

    fclose(cfPtr);
    }

    return 0;
    }


    ok....so that's what i have so far....but there's a couple of things i gotta work out...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-30-2004, 11:21 PM
  4. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 08:09 PM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 05:56 AM