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?
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.
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.
thank u how to read the next line in a c file
If it is a text file you can call ftell() just before reading the line for seeking back.
If you need the beginning of every line, those ftell() results can be stored in an array.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'; } }
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?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; }
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.
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.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.
Mid-file writes in text files often cause more problems than they solve.
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).
Last edited by quzah; 06-03-2011 at 03:29 PM.
Hope is the first step on the road to disappointment.
Of course