Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Problem processing a file

Posted on 2004-10-17
10
Medium Priority
?
227 Views
Last Modified: 2010-04-15
I have a while loop that calls a function that passes a pointer to a file.  The while loop calls one of two functions depending on the info it read from a file.  When it goes to the function it processses for more info from the file.  Then it repeats the process.  
The problem is when it repeats the process, it gets the same data as the last data it get from the file repeatedly.  Ex.  it got the number 2 for the last time through the first run of the function.  The next time it runs through the function every variable is assigned two.  The file stops being progressed through.  I am using fgets.  And calling the function by function(*FILE)
Any help would be appreciated
0
Comment
Question by:whinson
  • 5
  • 4
10 Comments
 
LVL 4

Expert Comment

by:pankajtiwary
ID: 12336250
Hi whinson,

As far as I can understand, the file pointer is not getting properly handled (probably seeking is not done properly). If you can post the relevent code , it would be easier for us to help you out.

Cheers!
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 12337480
Hi whinson,

Several things come to mind here.  Are you reading the file in each of the functions or are you trying to simply parse a line based on the content of the line?  I suspect that you're reading a line, and based on the content of the line calling a function to process the line.

How about posting the code so that we can get a better idea of where the problem lies?


Kent
0
 

Author Comment

by:whinson
ID: 12337724
Estentially the code is:
while(fscanf(in, "%d", x){
    fgets(buff, 256, in)
      if(x == a)
         function(in)
      else
         otherfunction(in)
     }
}

the function are very similar to one another
fgets(buff, 256, in)
x = atoi(strtok,buff, " \t...)
.
.
.
Pretty much repeats that until the end of the function

Thanks  
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 46

Accepted Solution

by:
Kent Olsen earned 750 total points
ID: 12337750

Ok.  In the "main" loop, you're performing the fscanf() before you do the fgets()?

That seems out of order...

Or perhaps *in* is global and the subfunctions 'preread' the next line?

Kent
0
 

Author Comment

by:whinson
ID: 12338466
The The format of the file i am reading in is
x(the number i want) then some words i dont care about
Ex.
'12 This is a Number'
I am using fscanf jus to get the 12 and the fgets to move through the rest of line to the next line
the function is
  void function(FILE*, int x)

the first the time the function is call the numbers are read correctly.  The problem is the 2nd time and so forth the function is called all the numbers read in end up being the same as the last number the first function read in.  
So:
1. First time running function
  x = 4
  y = 7
  z = 2
2.  Second time running function
  x = 2
  y = 2
  z = 2
3. Third time running the function
  x = 2
  y = 2
  z = 2
Etc. Etc.
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 12338654

How about posting the code and the first few lines of the data file?  That would make tracking this down a lot easier.  :)

It sounds like something really easy to fix, if I can just get to the source code.


kent
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 12338725

Are you trying to process the same file more than once?  If so you need to do an fseek() to rewind the file or close and reopen it.

Kent
0
 

Author Comment

by:whinson
ID: 12340311
No i am no processing the same file more than once, i am continually processing the file once in different functions.
while(fscanf(in, "%d", x) == 1){ //While it still gets one value
    fgets(buff, 256, in) //Read the rest of the line
      if(x == a) //if the one value is equal to a
         function(in) //call the function passing the file so the function can process the file
      else
         otherfunction(in) //else call the other function
     }
}

void function(*FILE){
Structure *struct;
malloc space for the struct;
fgets(buff,256, in)//read the next line in the function
struct->num = atoi(strtok,buff, " \t...); //get the numbers from the function
struct->num1 = atoi(strtok,buff, " \t...);
fgets(buff, 256, in) //read the next line
.
.
.
link the struct onto a global variable linked list
}


0
 

Author Comment

by:whinson
ID: 12340329
Input file looks like this
11
11 12 13
3 6 4
2 4
//This is where it should start over again
12
5 42
2 3 5
3
//star over again (these comments arent in the actual file)
etc.
etc.
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 12340347

Ok,
One of the things that jump out is the fscanf() statement.  It should be:

while(fscanf(in, "%d", &x) == 1)  /*  Note the '&' in front of x  */


Hard to tell what else could be wrong from the snippets.

Kent
0

Featured Post

Industry Leaders: 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

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 opening and writing to files 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.

971 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