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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 228
  • Last Modified:

Problem processing a file

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
whinson
Asked:
whinson
  • 5
  • 4
1 Solution
 
pankajtiwaryCommented:
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
 
Kent OlsenData Warehouse Architect / DBACommented:
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
 
whinsonAuthor Commented:
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.

 
Kent OlsenData Warehouse Architect / DBACommented:

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
 
whinsonAuthor Commented:
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
 
Kent OlsenData Warehouse Architect / DBACommented:

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
 
Kent OlsenData Warehouse Architect / DBACommented:

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
 
whinsonAuthor Commented:
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
 
whinsonAuthor Commented:
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
 
Kent OlsenData Warehouse Architect / DBACommented:

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now