Hello,

I have a file where the data layout is known. Suppose it's like this:
image1
SomeData
...
image1000
SomeData
...

I want to get an access to the line I want without the necessity to move across all the lines. Let's say I wanna access directly to the line "image627", is that possible?

void ReadFile( const char* file, uint32 image )
{
   char line[255], temp [64];
   while (fgets(line, sizeof line, file) != NULL) // read the file line by line
   {
     sprintf(temp, "image%u", image);
     if ( (strncmp(line, temp, 6) == 0) || (strncmp(line, temp, 7) == 0) || (strncmp(line, temp, 8) == 0) )
     {
       // do something
     }
   }
}

Thank you!

Recommended Answers

All 4 Replies

Only if each and every line is exactly the the same length. If they aren't, you have no way of knowing where any line starts.

If you're talking about the whole file, I say no. The lines do not have the same length.

But I have a repetition. The layout is:
line1: imageX -> it can be image0 until image 9999
line2: data: the length is not know

Thanks.

Seems to me you should be storing the data in a random access binary file. That way you could seek to the record you want. Or arrange the data in memory in such a structure after reading in the text file and then access it randomly by index or something.

Thank you. I'm gonna continue reading it line by line.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.