Thread: How to go into middle of file and read then write

  1. #1
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584

    How to go into middle of file and read then write

    I have this text file and I am searching for a string in a file. When I reach this string, I want to go one char beyond it (which will happen to be the next line) and then edit that line. I was fooling around with ftell and fseek, but that didn't seem to work. And help? Thanks...
    1978 Silver Anniversary Corvette

  2. #2
    Registered User breed's Avatar
    Join Date
    Oct 2001
    Posts
    91
    Does it have to be a text file ?
    Do you know how long the record is ?
    if not then open it in binary, then you can use fseek, ftell & fread.
    Before you judge a man, walk a mile in his
    shoes. After that, who cares.. He's a mile away and you've got
    his shoes.
    ************William Connoly

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Could also just load the file into memory, manipulate it there, and then write the whole thing back to disk. If the file is too large to fit in memory, use a small cache buffer - do a block read into it, search for your string, etc.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The problem with fseeking on text files is you dont know what the OS uses for line termination - you only ever see the converted answer, which is always \n

    The only reliable fseeks you can do on a text file are
    - seek to start
    - seek to end
    - seek to some previous result returned by ftell()

    Editing text files in-situ is always problematic, due to what happens when the new line is a different length to the old line.

  5. #5
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    This is what I tried to do with ftell and fseek but I got some really distorted output on the text file when I tried to write to it:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
    	FILE *fp;
    	char hw[] = "Homework #2";
    	char line[512];
    	char *test;
    	long i;
    
    	fp = fopen("test.txt", "r");
    
    	if (fp == NULL)
    	{
    		printf("Bad file\n");
    		return 0;
    	}
    
    	// read in the file
    
    	do {
    	test = fgets(line, sizeof(line), fp);
    	line[strlen(line)-1] = '\0';
    
    	if (strcmp(line, "<!--Comment1-->"))
    	{
    		continue;
    	}
    
    	else if (!strcmp(line, "<!--Comment1-->"))
    	{
    		i = ftell(fp);
    		break;
    	}
    
    	} while (test != NULL);
    	fclose(fp);
    
    	fp = fopen("test.txt", "w");
    
    	fseek(fp, i, SEEK_SET);
    
    	fputs(hw, fp);
    	return 0;
    }
    What I was trying to do here was read in until I found the string "<!--Comment1-->" (BTW I'm searching and editing a .html file -- I just put up a text file to test it with -- that is why I'm opening "test.txt" instead of an HTML file) and then store the value of the file pointer in 'i'. Then, I would close the file and open it for writing and goto the spot in the file where 'i' is stored as. This, apparently, didn't work. Any reason why?

    Should I just read the file into a char array buffer and go through that and edit? And then I could write the buffer back to the file. Suggestions? Thanks...
    1978 Silver Anniversary Corvette

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. SYnchronize read & write of a cache file
    By lei_michelle in forum C Programming
    Replies: 4
    Last Post: 02-26-2008, 05:49 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM