Thread: reading a file

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    21

    reading a file

    i am new to c,
    so i am writing this function that reads the content a file into an array of struct,, the code compiles perfectly fine but when i run the program it outputs something like this..
    "Gj1323193950166804700000000000000000000000000000 000000000000000000000000000000"

    so here is my code.,
    Code:
    /*************
    test.h
    **************/
     typedef struct{
        char commandName[10];
        double comValue;
    }fileCommand;
    Code:
    /*************
    test.c
    **************/
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #include "test.h"
    
    FILE* f;
    
    //prototype
    int numLine(FILE* fileName);
    
    
    int main(int argc, char* argv[]){
        int lenght, i;
        f = fopen(argv[1], "r+");
        
        if( f != NULL){
            lenght = numLine(f);
            printf("number of lines : %d\n", lenght);
            fileCommand* fCommand = malloc(sizeof(*fCommand) * lenght);
            
            for(i=0; i<(lenght); i++){
                fscanf(f, "%s %lf\n", &(fCommand->commandName), &(fCommand->comValue));
                printf("%s %lf", fCommand->commandName, fCommand->comValue);
    
    
            }
            
        fclose(f);        
        
        free(fCommand);
        }
        else{
            perror("error opening file");
        }
        return 0;
        
        
    }
    
    
    int numLine(FILE* fileNames){
        int numLines=0;
        char ch;
            do{
                ch = fgetc(fileNames);
                if(ch == '\n')
                numLines++;
            } while (ch != EOF);
    
    
            if(ch != '\n' && (numLines) != 0){ 
                numLines++;
            }
        return numLines;
    }
    the content of the file i am trying to read is something like this:

    hello 56
    programming 32
    windows 32


    CHEERS..

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Your numlines() function reads until the end of the file. Then, starting from end of file, your main() function attempts to read multiple lines. main() is not doing any error checking, so will keep looping even if fscanf() reports an error - which it will, from the first call.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    21
    so once i have read until the end of the file using numlines(),, how do i get back to the start of the file??

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    21
    so i added
    Code:
    rewind(fileNames);
    before
    Code:
    fclose(fileNames);
    in the numLines()... but it still outputs something like this..
    "number of lines : 5
    x7; 17513790332202264000000000000000000000000000000000 00000000000000000000000000"..
    PLEASE HELP!

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You read the file.
    You rewind()
    You read the file again.

    Think about where you placed the rewind() call.
    Was it between the two passes at reading the file?
    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.

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    21
    Code:
        if( f != NULL){
            lenght = numLine(f);
            printf("number of lines : %d\n", lenght);
            rewind(f);
            fileCommand* fCommand = malloc(sizeof(*fCommand) * lenght);
    is this correct? !

  8. #8
    Registered User
    Join Date
    Sep 2012
    Posts
    21
    I am still getting some random output,, like i mentioned in post #1,,,, please help..!!

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    $ gcc foo.c
    foo.c: In function ‘main’:
    foo.c:31:13: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘char (*)[10]’ [-Wformat]
    $ ./a.out foo.txt
    number of lines : 4
    hello 56.000000
    programming 32.000000
    windows 32.000000
    windows 32.000000
    $ cat foo.txt
    hello 56
    programming 32
    windows 32
    1. Your scanf format is wrong - you don't need the & when using %s
    2. Your line count is wrong - it seems to count an extra line. This leads to the last record being duplicated.
    3. "programming" is longer than the 9 (+1 for \0) you allow in your struct, which will trash 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.

  10. #10
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Or you could simply close and open the file again after the first read is through.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  11. #11
    Registered User
    Join Date
    Sep 2012
    Posts
    21
    thank you all,, @salem, the numLines() works fine for me.. it gives me the exact number of lines..
    @waltP, closing and opening the file worked,, thanks alot...

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You sound surprised!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-17-2011, 03:51 AM
  2. Replies: 13
    Last Post: 05-31-2009, 11:30 AM
  3. File I/O (Reading from a Random-Access File)
    By VersEtreOuNe in forum C++ Programming
    Replies: 25
    Last Post: 02-14-2008, 05:13 AM
  4. Reading in a binary file from offset into another file
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 05-24-2006, 03:01 AM
  5. Reading flat file and generating tagged file
    By AngKar in forum C# Programming
    Replies: 4
    Last Post: 03-24-2006, 08:29 AM