fseek(), moving ptr to specific line after every loop.

This is a discussion on fseek(), moving ptr to specific line after every loop. within the C Programming forums, part of the General Programming Boards category; Hi I have a array of numbers in a txt file as shown below 139 344 140 345 139 344 ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    3

    fseek(), moving ptr to specific line after every loop.

    Hi

    I have a array of numbers in a txt file as shown below

    139
    344
    140
    345
    139
    344
    140
    345

    I want to read 2numbers fore every loop.(139.344 for first loop. 140.345 for 2nd loop)
    I am using fseek(file,(j-1)*6,SEEK_SET);
    wher j-1 give 3,5,6..

    but I am not able to read the correct numbers fore every-loop. It read first 139,344 correctly and then 0, it is not pointing he correct line.
    can anybody help me with this.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Why mess with the file pointer at all? Won't it be exactly where you want it at the start of the next go-around, or are you trying to skip around in the file? (And if you do want to do fseek, j should be 0, 1, 2, etc.)
    Last edited by tabstop; 07-25-2011 at 02:59 PM.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mbr123 View Post
    Hi

    I have a array of numbers in a txt file as shown below

    139
    344
    140
    345
    139
    344
    140
    345

    I want to read 2numbers fore every loop.(139.344 for first loop. 140.345 for 2nd loop)
    If you are using the numbers sequentially, read the file sequentially using fscanf() or fgets() don't mess with the file pointer. In text files fseek() is extremely unreliable due to the possibility of hidden characters (trailing tabs, trailing spaces, cr/lf, etc.) fseek() is useful only in records based files using structs (sometimes called random access files).

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    3
    I use fopen to open the text file
    and I want to read every 2number for a loop, and read next set of 2number for 2nd loop, How Can I do that?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Just as you say. Make a loop, and inside the loop read two numbers (using fscanf or fgets at your leisure).

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mbr123 View Post
    I use fopen to open the text file
    and I want to read every 2number for a loop, and read next set of 2number for 2nd loop, How Can I do that?
    You can't.... not and have it work reliably. As I already pointed out fseek() is totally unreliable in text files it is intended for fixed size records, not variable length lines.

    Why don't you read them into two arrays in one loop... something like this...
    Code:
    int EvenLines[MaxLines/2] = {0};
    int OddLines[MaxLines/2] = {0};
    int idx = 0;
    
    FILE f = fopen("textfile","r")
    while ((!eof(f)) && (idx < MaxLines/2)))    
      { fscanf("%d",EvenLines[idx]);
         fscanf(%d",OddLines[idx]);
         idx++; }
    Now you have two separate arrays one with lines 0, 2, 4, etc the other with lines 1, 3, 5, etc.

    Or you could just read them all into one array and access them by odd and even indexes.

    @adak... you're faster... mines better

  7. #7
    Registered User
    Join Date
    Jul 2011
    Posts
    3
    I will explain you my situation

    I was trying to change the start position of the txt file, so that every time the file open, it start position is set to the number I require and the file is read from that point.
    I want to have that accessibility,
    In my code, I can read the number I require once the start point in the file is set,
    So now I tired to set the start point using fseek as I mentioned before. but now I have a problem using it for 3 digit numbers.

    sgfile = fopen(params->SliceGroupConfigFileName,"r");
    fseek(sgfile,(j-1)*4,SEEK_SET);
    ret = fscanf(sgfile,"%d",(params->top_left+i)); //1st number
    ret = fscanf(sgfile,"%d",(params->bottom_right+i)); //2nd number

    so I am trying to change the start position of the text file for ever loop.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    2,770
    Quote Originally Posted by mbr123 View Post
    Code:
    sgfile = fopen(params->SliceGroupConfigFileName,"r");
    Try to open in binary mode.

    If I recall correctly like this
    Code:
    sgfile = fopen(params->SliceGroupConfigFileName,"rb");
    Note: This will change how file operations work.

    Tim S.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    fseek isn't really made for text files. And if you change any of your numbers at all, so they have more or less digits, it's going to ruin anything you've just done for keeping track of where to seek. Why aren't you just using binary, and storing the number that way? It's going to work out the same anyway, and it will give you a bigger range of numbers. Actually, once you break three digits (two, depending if you use \n or \r or \n + \r on your OS for your new line info), it's going to work out better:

    123<NL> = four (or five) bytes
    <write in binary one four-byte int> = four bytes (assuming four byte ints) + lets you have big numbers


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by mbr123 View Post
    So now I tired to set the start point using fseek as I mentioned before. but now I have a problem using it for 3 digit numbers.
    I'm not sure what you are missing here:

    Quote Originally Posted by CommonTater
    In text files fseek() is extremely unreliable due to the possibility of hidden characters (trailing tabs, trailing spaces, cr/lf, etc.) fseek() is useful only in records based files using structs (sometimes called random access files).
    and then again:

    Quote Originally Posted by CommonTater
    You can't.... not and have it work reliably. As I already pointed out fseek() is totally unreliable in text files it is intended for fixed size records, not variable length lines.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mbr123 View Post
    I will explain you my situation

    I was trying to change the start position of the txt file, so that every time the file open, it start position is set to the number I require and the file is read from that point.
    I want to have that accessibility,
    In my code, I can read the number I require once the start point in the file is set,
    So now I tired to set the start point using fseek as I mentioned before. but now I have a problem using it for 3 digit numbers.

    sgfile = fopen(params->SliceGroupConfigFileName,"r");
    fseek(sgfile,(j-1)*4,SEEK_SET);
    ret = fscanf(sgfile,"%d",(params->top_left+i)); //1st number
    ret = fscanf(sgfile,"%d",(params->bottom_right+i)); //2nd number

    so I am trying to change the start position of the text file for ever loop.
    Ok... what part of "forget it" do you not understand?

    First of all a 3 digit number in a text file isn't 3 characters... it's like 5... On Windows systems "123" will appear in the file as... 123\r\n... that is the 3text characters, a line feed and a carriage return. On Linux it will appear as 123\r. Some Mac and BSD systems will have it as 123\n ... Then there's the problem of trailing spaces... you might actually have 123<space>\r\n ... or 123\t\r\n...

    As I explained before... fseek() just plain isn't going to work on a text file. Read the file into an array and pick the array element you need... it's about the only way you're going to make this work.
    Last edited by CommonTater; 07-26-2011 at 11:50 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read A File From A Specific Line
    By dsured in forum C Programming
    Replies: 8
    Last Post: 03-16-2011, 09:56 PM
  2. fseek question regarding line number
    By DB66 in forum C Programming
    Replies: 1
    Last Post: 10-18-2008, 08:23 PM
  3. Moving cursor to the end of line
    By behzad_shabani in forum C Programming
    Replies: 5
    Last Post: 05-30-2008, 01:41 PM
  4. moving up a line or more
    By alexnb185 in forum C Programming
    Replies: 3
    Last Post: 08-19-2007, 04:24 PM
  5. moving a line in videomode 320*200
    By GanglyLamb in forum Game Programming
    Replies: 4
    Last Post: 12-27-2002, 05:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21