editing a txt document

This is a discussion on editing a txt document within the C Programming forums, part of the General Programming Boards category; Hi, I'm trying to edit a single number inside a text document. For example a document like this: row1: 12 ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    14

    editing a txt document

    Hi,

    I'm trying to edit a single number inside a text document.

    For example a document like this:

    row1: 12
    row2: 34
    row3: 32
    row4: 23


    I just want to be able to change these numbers.

    I've created text files before with:

    FILE *fp = fopen("filename", "w");
    .
    .
    fclose(fp);


    I assume I'll use this again but I don't know how to modifiy just one thing.

    Any advice is appreciated,
    OJ

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    You edit a single part of the file. You will have to open the file in read mode, read the whole file into memory, then edit it, then write to a file in write mode.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    14
    Okay,
    Sorry for the stupid question but how do I read it into memory?
    OJ

  4. #4
    Unregistered User
    Join Date
    Nov 2004
    Posts
    25
    Look for fscanf()

  5. #5
    ---
    Join Date
    May 2004
    Posts
    1,379
    If you use fgets() you can read it into an array 1 line at a time.

    Code:
    #include <stdio.h>
    #define SIZE 80
    
    int main(void)
    {
      FILE *in;
      in = fopen("input.txt","r");
      char filename[SIZE];
      
      fgets(filename,SIZE,in);
    
      return 0;
    }
    Whack a loop in there and test for EOF and your ready to edit

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    14
    Okay,
    I think I've sort of got it into memory now but there are still problems. When I try to print out the file that I've just got, it only gives me the last row.

    And then I try to change a character and write the whole file back. However this just erases the original documnet.

    Here is the code

    Code:
    int main()
    {	
    	// Read file into memory
    	FILE *in = fopen("file_to_edit", "r");
    	for (i=0; i<3; i++)
    	{
    	fgets(filename, SIZE,  in);
    	printf( "got this from document: %s\n", filename);
    	}
    	fclose(in);
    	
    	// Print out what i got	
    	for (i=0; i<21; i++)
    	{
    	printf("%c", filename[i]);
    	}
    		
    	// Change a value and store back into document
    	FILE *in2 = fopen("file_to_edit", "w");
    	filename[7] = 9;
    	for(i = 0; i<10; i++);
    	{
    	fprintf(in2,"%c\n",filename[i]);
    	}
    	fclose(in2);
    	
    	return(0);
    }
    I'm sure the problems are more than obvious to your average programmer, however I'm still a newbie so any help is appreciated.

    OJ

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Are you using global variables here, or what? Because I'm not seeing filename shown in your code before you start using it. Also, why is it called filename, when in fact it isn't the file's name at all?

    Also, I'm assuming that filename is an array of characters that you've got some place. If so, this assignment is incorrect:
    Code:
    filename[7] = 9;
    What you're doing is assigning the decimal value 9 to your array, which is not in fact the character '9'. If you want it to be the character '9', then you do need to enclose it in single quotation marks:
    Code:
    filename[7] = '9';
    Also, here:
    Code:
    for(i = 0; i<10; i++);
    	{
    	fprintf(in2,"%c\n",filename[i]);
    	}
    it appears as though you're just writing a line of one character + the newline to a file. This will only print one single character. I believe what you want is a file which has one number per line. Correct?

    If so, you've got more than a few problems.

    1) You're reading the line wrong. You should look at combining sscanf with fgets to get the number from the file. (This assumes your filename variable is an array of integers, which it appears is what you're trying to do, now that I look at your code again.)

    2) When you print the numbers, use something like fprintf, using the "%d", specifier. Something like so:
    Code:
    for( x = 0; x < SOMENUMBER; x++ )
        fprintf( outputfile, "%d\n", yourdata[ x ] );
    That should get you started in the right direction.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wierd txt document
    By Necrofear in forum Tech Board
    Replies: 4
    Last Post: 05-22-2006, 10:24 PM
  2. Editing TXT files in C++
    By Nexus in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2005, 05:34 AM
  3. Document Class and Dialog Windows :: MFC
    By kuphryn in forum Windows Programming
    Replies: 3
    Last Post: 12-01-2002, 12:27 AM
  4. MultiDocument And MultView (and per document) :: MFC
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 04-09-2002, 10:00 AM
  5. Getting document point co-ordinates
    By swordfish in forum C++ Programming
    Replies: 2
    Last Post: 09-05-2001, 01:37 PM

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