Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Error reading from a file

Posted on 2003-03-17
3
Medium Priority
?
169 Views
Last Modified: 2010-04-15
Hi, I have the following code for reading in some data (sample data shown later):

  f=fopen("lecturers.txt","r");

  if (f==NULL) {
    printf("File failed to open\n");
    exit(EXIT_FAILURE);
  }

  do {

    numlecturers++;

    c = fscanf(f,"%s",oneword);
    if (oneword != NULL) {
      strcpy(lecturer[numlecturers].Name,oneword);
      printf(lecturer[numlecturers].Name);
     printf("\n");
    }

    c = fscanf(f,"%s",oneword);
    if (oneword != NULL) {
      strcpy(lecturer[numlecturers].BeautyOccupation,oneword);
      printf(lecturer[numlecturers].BeautyOccupation);
      printf("\n");
    }

    c = fscanf(f,"%i",&tempint);
    strcpy(lecturer[numlecturers].bookings[tempint].AppointmentName,"L");
    printf("%i\n",tempint);

  } while (!feof(f));

  fclose(f);

===========================================================
SAMPLE DATA

JVT
Crimp_and_Perm
2
NAH
Manicurist
3
CJW
Non_Surgical_Facelifts
4
MWJ
Pedicures
5
PWG
Ear_Piercing
1
JAS
Leg_Waxing
2

=====================================================
PROBLEM

However when the program reads in the data it, it reads it into the array as follows:

JVT
Crimp_and_Perm
2
NAH
Manicurist
3
CJW
Non_Surgical_Facelifts
4
MWJ
Pedicures
5
PWG
Ear_Piercing
1
JAS
Leg_Waxing
2
Leg_Waxing
Leg_Waxing
2


Any help would be greatly appreciated

Thanks

Dan
0
Comment
Question by:danny1979
  • 2
3 Comments
 
LVL 8

Expert Comment

by:akshayxx
ID: 8152665
http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_20552881.html

look at this today only some one else asked similar problem
0
 
LVL 16

Expert Comment

by:imladris
ID: 8152669
The problem is that getting an EOF result only happens when you run into trouble. That is, when the program ran JAS, Leg_Waxing, 2 (in its last proper iteration) no EOF condition existed. The fscanf function read the next integer (2) as requested, and everything was fine. It is not until it gets back to the top of the loop and is attempting to read the next lecturer that it discovers there isn't anything more in the file, and so an EOF occurs.

One simple solution would be to check the return from fscanf for the lecturer, and, if it is 0, break out of the loop.
0
 
LVL 8

Accepted Solution

by:
akshayxx earned 120 total points
ID: 8152679
may be u cant see  PAQ's

 so i'll paste the comment that i made there .. u can use it with modification as per ur needs


FILE *fptr;
char doc[200];
fptr = fopen("list.dat", "r+");

while(!feof(fptr))
{
fgets(doc, 200, fptr);
if(!feof(fp))   // <------- small change
printf("%s", doc);  //<-- removed '\n'
else break;    // <-- else break out of loop
}
fclose(fptr);

actually EOF flag is set at the first unsuccessful read .. thats the reason why u have to check before printf

OR easier way .. since u r using fgets.. check in the man page its return value

while(fgets(doc, 200, fptr))
{
printf("%s", doc);  //<-- removed '\n'
}
fclose(fptr);


when using fgets ..  following is enough .. no need to check for eof also ..

while(fgets(doc, 200, fptr))
{
printf("%s", doc);
}

for the following reason
gets() and fgets() return s on success, and NULL on error or  when  end
      of file occurs while no characters have been read.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question