Solved

Problem processing a file

Posted on 2004-10-17
10
209 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 45

Expert Comment

by:Kdo
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
 
LVL 45

Accepted Solution

by:
Kdo earned 250 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 45

Expert Comment

by:Kdo
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 45

Expert Comment

by:Kdo
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 45

Expert Comment

by:Kdo
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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 recursion in the C programming language.

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now