?
Solved

Error reading from a file

Posted on 2003-03-17
3
Medium Priority
?
168 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

764 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