Thread: problem with File

  1. #1
    Registered User
    Join Date
    Oct 2001

    problem with File


    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);
    printf("values added......");
    fprintf(stderr,"eror opening file");


    my records are like this:

    empno first name last name

    012452 Wong Andrew

    891756 Simmons Angela

    268174 Tindall Jeremy


  2. #2
    Registered User
    Join Date
    Aug 2001

    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
    The edge of the known universe
    This should suffice for simple use

    while ( fscanf(fp,"%d %s %s",&no, fname, lname) != EOF ) {
        printf("values added......");
    But this is better
    char buff[BUFSIZ];  // BUFSIZ in stdio.h
    while ( fgets( buff, BUFSIZ, fp ) != NULL ) {
        if ( sscanf( buff, "%d %s %s", &no, fname, lname) == 3 ) {
            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.

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, 04: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, 03:44 PM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Rename file problem
    By Emporio in forum C Programming
    Replies: 2
    Last Post: 06-05-2002, 09:36 AM