Solved

File handling

Posted on 2002-03-13
8
253 Views
Last Modified: 2012-05-07
I have problems handling file:

1) Look at this piece of code :

while(!feof(filepointer)
{

   fscanf(filepointer,"%s",name);
}

File has 5 name like this :
name1
name2
name3
name4
name5

This while loop reads till name4. then reads name5.
Now, it should stop here.
But it goes and reads name 5 again and then comes out.
I do not understand how.

2)In my program I am writing some thing into a file.
while this process is going on, I am not able to see anything written to the file using VI editor.
I am doing fflush() soon after I write to the file.
Please help
0
Comment
Question by:kgreddy
[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
8 Comments
 
LVL 6

Accepted Solution

by:
Triskelion earned 50 total points
ID: 6860422
The problem is that you do not get the determination of eof() until after the next fscanf().

Try something BRUTAL like this

while (!feof(filepointer))
{
   fscanf(filepointer, "%s", strSomeTempString);
   if (!feof(filepointer))
      {
      //copy to target string
      }
}

or

use gets()
since you're just dealing with one string
 
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6860424
make that fgets()
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6860452
This works as expected:

#include <stdio.h>

int main(void)
{
     FILE * hanInFile=NULL;
     char     strData[16] = {0};
     if (NULL == (hanInFile=fopen("..\\20276268.txt", "rb")))
          {
          puts ("cannot open input file");
          return 1;
          }

     while (!feof(hanInFile))
          {
          fgets(strData, sizeof(strData), hanInFile);
          puts(strData);
          }
     fclose(hanInFile);
     return 0;
}
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:Triskelion
ID: 6860469
...but then again, so does this:
#include <stdio.h>

int main(void)
{
     FILE * hanInFile=NULL;
     char     strData[16] = {0};
     if (NULL == (hanInFile=fopen("..\\20276268.txt", "r")))
          {
          puts ("cannot open input file");
          return 1;
          }

     while (!feof(hanInFile))
          {
          /*fgets(strData, sizeof(strData), hanInFile);*/
          fscanf(hanInFile, "%s", strData);
          puts(strData);
          }
     fclose(hanInFile);
     return 0;
}
/*
Hmmm...
Are you sure you don't have trailing spaces or an end-of-file marker floating around somewhere?
*/
0
 
LVL 1

Expert Comment

by:Shivshankar
ID: 6867345
I see a simple solution.
     FILE * fp;
     .
     .
     .
     fscanf(fp, "%s", name);
     while(!feof(fp)) {
          printf("%s\n", name);
          fscanf(fp, "%s", name);
     }

This works fine in all the 3 compilers I have.
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6888434
kgreddy, have you tried any of these?
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6892774
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if still open in seven days.  Please post closing recommendations before that time.

Question(s) below appears to have been abandoned. Your options are:
 
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you. You must tell the participants why you wish to do this, and allow for Expert response.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for experts of your intention to delete and why
   --> You cannot delete a question with comments, special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the question QID/link(s) that it regards.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click the Help Desk link on the left for Member Guidelines, Member Agreement and the Question/Answer process.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Please click you Member Profile to view your question history and keep them all current with updates as the collaboration effort continues, to track all your open and locked questions at this site.  If you are an EE Pro user, use the Power Search option to find them.  Anytime you have questions which are LOCKED with a Proposed Answer but does not serve your needs, please reject it and add comments as to why.  In addition, when you do grade the question, if the grade is less than an A, please add a comment as to why.  This helps all involved, as well as future persons who may access this item in the future to seek help.

To view your open questions, please click the following link(s) and keep them all current with updates.
http://www.experts-exchange.com/questions/Q.11473240.html
http://www.experts-exchange.com/questions/Q.11667758.html
http://www.experts-exchange.com/questions/Q.20076226.html
http://www.experts-exchange.com/questions/Q.20107139.html
http://www.experts-exchange.com/questions/Q.20109162.html
http://www.experts-exchange.com/questions/Q.20109691.html
http://www.experts-exchange.com/questions/Q.20118121.html
http://www.experts-exchange.com/questions/Q.20118127.html
http://www.experts-exchange.com/questions/Q.20173258.html
http://www.experts-exchange.com/questions/Q.20240985.html
http://www.experts-exchange.com/questions/Q.20258498.html
http://www.experts-exchange.com/questions/Q.20260199.html
http://www.experts-exchange.com/questions/Q.11263994.html
http://www.experts-exchange.com/questions/Q.20270368.html
http://www.experts-exchange.com/questions/Q.20271874.html
http://www.experts-exchange.com/questions/Q.20274619.html
http://www.experts-exchange.com/questions/Q.20276268.html
http://www.experts-exchange.com/questions/Q.20276453.html
http://www.experts-exchange.com/questions/Q.20276277.html


To view your locked questions, please click the following link(s) and evaluate the proposed answer.
http://www.experts-exchange.com/questions/Q.20271075.html

PLEASE DO NOT AWARD THE POINTS TO ME.  
 
------------>  EXPERTS:  Please leave any comments regarding your closing recommendations if this item remains inactive another seven (7) days.  Also, if you are interested in the cleanup effort, please click this link http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643

Moderators will finalize this question if still open in 7 days, by either moving this to the PAQ (Previously Asked Questions) at zero points, deleting it or awarding expert(s) when recommendations are made, or an independent determination can be made.  Expert input is always appreciated to determine the fair outcome.
 
Thank you everyone.
 
Moondancer
Moderator @ Experts Exchange

P.S.  For any year 2000 questions, special attention is needed to ensure the first correct response is awarded, since they are not in the comment date order, but rather in Member ID order.
0
 
LVL 5

Expert Comment

by:Netminder
ID: 6973859
Admin notified of User neglect. Force-accepted by
Netminder
CS Moderator
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Read file descriptor 3 in C 14 128
Detect CR LF to each line 12 185
Problem to scan all sheets 3 135
Secondary drive is failing... D Volume 11 98
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…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

751 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