How to go to the begining of a line in a file in c

This is a discussion on How to go to the begining of a line in a file in c within the C Programming forums, part of the General Programming Boards category; How to go to the begining of a line in a file in c?...

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

    How to go to the begining of a line in a file in c

    How to go to the begining of a line in a file in c?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Frankly if it's a text file you're pretty much buffaloed. fseek() allows you to reposition the file pointer, but you got to know where you're aiming for... You can use scanning techniques such as "find next line end" or "find previous line end" bouncing along the CR/LF pairs but they are going to be horrifically slow. My usual tactic with text files is to load the entire thing into memory as one long buffer and skip across it with strtok() or wcstok() building an array of pointers to each line. Do what I have to do to it, then re-compose and re-write the whole file.

    Things get better if it's a binary file. These are usually written with a known structure that can be moved along by calculation.

    So... first question ... what's in the file? Is it plain text, unicode text, binary data???
    Next... what have you tried already? Post your code, maybe we can help.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    You can read any line at random a second time, so long as you record the ftell() position you were at the first time you read it.

    To read a line again, you simply fseek() to the previously stored ftell position.

    So if you have a large file you want to dip into every so often, it might be worth scanning it (using say fgets), and record all the ftell positions for all of the lines.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    3

    how to read the next line in a c file

    thank u how to read the next line in a c file

  5. #5
    Registered User Inanna's Avatar
    Join Date
    May 2011
    Posts
    69
    If it is a text file you can call ftell() just before reading the line for seeking back.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void strip_linebreak(char* s);
    
    int main()
    {
        FILE* fp = fopen("test.txt", "r");
    
        if (fp)
        {
            char line[BUFSIZ];
            int choice;
            long pos;
    
            while (1)
            {
                pos = ftell(fp);
    
                if (!fgets(line, BUFSIZ, fp))
                {
                    break;
                }
    
                strip_linebreak(line);
    
                puts(line);
                printf("1) Re-read line\n2) Next line\n: ");
                
                if (scanf("%d", &choice) != 1)
                {
                    break;
                }
    
                if (choice == 1)
                {
                    fseek(fp, pos, SEEK_SET);
                }
            }
        }
    }
    
    void strip_linebreak(char* s)
    {
        int back = strlen(s)-1;
    
        if (s[back] == '\n')
        {
            s[back] = '\0';
        }
    }
    If you need the beginning of every line, those ftell() results can be stored in an array.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    3
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    int main(){
    FILE *fp;
    char c;
    char str[1000];
    char name[10]="mario";
    fp=fopen("df.txt","r+");
    fputs("sdsdnsd",fp);
    fgets(str,100,fp);
    //fseek(fp,6,SEEK_SET);
    fprintf(fp,"\r bhuvan %s",name);
    fputs("/r",fp);
    fputs("India",fp);
    fclose(fp);
    return 0;
    
    }
    this is what i've tried.... i also want to go to the next line suppose i'm in 1st line i want go to 2nd line...how can i do tat?

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    First you should note that changing the length of a line, half way through a text file is going to cause you all kinds of problems. Take your test code, run it on your file... then open the file in a text editor... you may not like the result.
    Code:
    // original text
    
    My name is Luka 
    and I can be a friend to you.
    
    // replace Luka with Mitchel  by writing mid file
    
    My name is Mitchelnd I can be a friend to you.
    Contrary to our friends advice, who I'm sure are thinking about simply reading the the file, I will advise, as before that you load the whole file tokenize it into strings (one string per line) work on it in memory using string manipulation functions, then write the whole file back out when done.

    Mid-file writes in text files often cause more problems than they solve.

  8. #8
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Perhaps you could tell us what you are trying to do.
    All reading functions update the file position.
    Thus eg fgets() will read untill newline or EOF is met and next call will start from where the file position is left off(i.e next line).

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Bayint Naung View Post
    Thus eg fgets() will read untill newline or EOF is met and next call will start from where the file position is left off(i.e next line).
    This is only true if your buffer is bigger than the longest line in the file. If your line doesn't contain a newline before it fills up, it won't be at the start of a new line:
    Code:
    char buf[ 6 ];
    ...
    fgets( buf, 6, file );
    ...
    /* file: */
    1234567<newline>

    Quzah.
    Last edited by quzah; 06-03-2011 at 03:29 PM.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Of course

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 12-13-2010, 01:13 PM
  2. binary tree begining, need help
    By ivan12yu2000 in forum C++ Programming
    Replies: 3
    Last Post: 05-14-2009, 12:01 AM
  3. how to add a note to the begining of linked list
    By merixa in forum C++ Programming
    Replies: 8
    Last Post: 10-24-2005, 02:10 AM
  4. Go to the begining of file (new one)
    By Downnin in forum C++ Programming
    Replies: 1
    Last Post: 08-27-2005, 01:39 PM
  5. Ignoring the Tab at the begining of the line..
    By NANO in forum C++ Programming
    Replies: 8
    Last Post: 05-03-2003, 10:56 PM

Tags for this Thread


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