problem with File

This is a discussion on problem with File within the C Programming forums, part of the General Programming Boards category; hello my problem is "how to trace file" . when i read from file it reads only first recod how ...

  1. #1
    anu
    anu is offline
    Registered User
    Join Date
    Oct 2001
    Posts
    9

    problem with File

    hello

    my problem is "how to trace file" .
    when i read from file it reads only first recod how can i read second record and so on. i tried feof () also but didn't work.

    my code is like this

    if ((fp=fopen("emp.txt","r"))!= NULL)
    {
    //how can i use loop here?

    fscanf(fp,"%d %s %s ",&no, fname, lname);
    add(no,fname,lname);
    printf("values added......");
    }
    else
    {
    fprintf(stderr,"eror opening file");


    exit(1);
    }

    my records are like this:

    empno first name last name

    012452 Wong Andrew

    891756 Simmons Angela

    268174 Tindall Jeremy

    thanx.

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Hi,

    its not a good idea to loop while testing if file opens okay, as the file would be opened as new every loop.

    also your employee number is out of range to be stored as an int, you can either read this as a string, not a bad idea as the emp num is permanent and is not required in any calculations. Either that you will have to store employee number as a double.

    unless you format your fscanf for len of string, then the first %s will read rest of file until a null char is encountered e.g. it will read the space characters between first and surnames.

    Is each employee record seperated by a newline character, you will have to determin how each record on the file is seperated, if at all. Would be better to read file character at a time, checking for EOF and space between fields.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    This should suffice for simple use

    Code:
    while ( fscanf(fp,"%d %s %s",&no, fname, lname) != EOF ) {
        add(no,fname,lname);
        printf("values added......");
    }
    But this is better
    Code:
    char buff[BUFSIZ];  // BUFSIZ in stdio.h
    while ( fgets( buff, BUFSIZ, fp ) != NULL ) {
        if ( sscanf( buff, "%d %s %s", &no, fname, lname) == 3 ) {
            add(no,fname,lname);
            printf("values added......");
        } else {
            // invalid line format error
        }
    }
    > then the first %s will read rest of file until a null char is encountered e.g. it will read the space characters between first and surnames.
    This is incorrect - %s stops at the first white space (or newline)
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 05:27 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 04:39 AM
  5. Rename file problem
    By Emporio in forum C Programming
    Replies: 2
    Last Post: 06-05-2002, 10:36 AM

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