6 Replies - 9896 Views - Last Post: 31 December 2007 - 05:51 PM Rate Topic: -----

#1 anomani  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 30-December 07

Edit text file using c++

Post icon  Posted 30 December 2007 - 09:21 PM

Hi

I have following format of text file. I want to edit this text file using C++

1001 1001 0 0 10000
1001 1001 1 500 9500
1002 1002 0 0 20000
1003 1003 0 0 30000
1001 1001 2 600 10100
1002 1002 1 700 19300

I want to replace the value of fifth row and last colum with 8000
How can I do using c++ code. I tried alot but could not do.
Is This A Good Question/Topic? 0
  • +

Replies To: Edit text file using c++

#2 Carth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 30-December 07

Re: Edit text file using c++

Posted 30 December 2007 - 11:48 PM

You could try reading your values into a 2D integer array, from your text file. Then make the change of 8000 to array[4][4]. Then overwrite the file again. I'm not sure exactly what you mean, but this is an easy way.

FILE* fp = fopen("filename.txt","r");
int temp=0;
int array[5][5];
int i=0,j=0;
while(  fscanf( fp, "%d", &temp ) != EOF ) {

		array[i][j] = temp;
	j++;
	if(j==5) {
			j=0;
		i++;
	}
}
fclose(fp);
array[4][4] = 8000;
fp = fopen("filename.txt","w");
i=0;
while(i<6) {
	fprintf( fp, "%d %d %d %d %d\n", array[i][0], array[i][1], array[i][2], array[i][3], array[i][4] );
		i++;
}



This post has been edited by Carth: 31 December 2007 - 12:10 AM

Was This Post Helpful? 0
  • +
  • -

#3 Tom9729  Icon User is offline

  • Segmentation fault
  • member icon

Reputation: 180
  • View blog
  • Posts: 2,641
  • Joined: 30-December 07

Re: Edit text file using c++

Posted 31 December 2007 - 03:06 PM

View PostCarth, on 30 Dec, 2007 - 11:48 PM, said:

You could try reading your values into a 2D integer array, from your text file. Then make the change of 8000 to array[4][4]. Then overwrite the file again. I'm not sure exactly what you mean, but this is an easy way.

FILE* fp = fopen("filename.txt","r");
int temp=0;
int array[5][5];
int i=0,j=0;
while(  fscanf( fp, "%d", &temp ) != EOF ) {

		array[i][j] = temp;
	j++;
	if(j==5) {
			j=0;
		i++;
	}
}
fclose(fp);
array[4][4] = 8000;
fp = fopen("filename.txt","w");
i=0;
while(i<6) {
	fprintf( fp, "%d %d %d %d %d\n", array[i][0], array[i][1], array[i][2], array[i][3], array[i][4] );
		i++;
}



Might just be me, but that's C (not C++), and it segfaults when I run it.

anomani, there are a lot of ways you could do this. If you could post your code we could probably tell you what's wrong, and help you fix it. :)
Was This Post Helpful? 0
  • +
  • -

#4 Jingle  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 317
  • Joined: 20-October 07

Re: Edit text file using c++

Posted 31 December 2007 - 03:47 PM

if your using c++ look into fstream.h
it should have somthing on google about it
Was This Post Helpful? 0
  • +
  • -

#5 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 45
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Edit text file using c++

Posted 31 December 2007 - 04:17 PM

The code posted will compile as C++, but it doesn't really hold to the OO principles that you'd look for in a pure C++ program. For that, you'd want to go with file streams, as Jingle mentioned. However, fstream.h is a deprecated header, and is only included for backwards compatability with older code. You should probably use just fstream, without the file extension.

The easiest way to do this would probably be to read the original file in line by line, and output it to a new temporary file, making the modification to the appropriate entry. Outputting the unmodified line to the file won't be a problem, but you may want to try using string streams when it comes time to outputting a modified value. Then, once the entire file has been transferred, delete the original and rename the temporary file.

Some refs that might be helpful for you:

file streams: http://www.cplusplus...stream/fstream/
string streams: http://www.cplusplus...m/stringstream/
strings: http://www.cplusplus...ference/string/ (especially the getline( istream& is, string& str, char delim ) global function)
Was This Post Helpful? 0
  • +
  • -

#6 Jingle  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 317
  • Joined: 20-October 07

Re: Edit text file using c++

Posted 31 December 2007 - 05:38 PM

my programing teacher uses borland and an old version to.
fstream.h is about the only thing i have learned in one semester of that class so far. I have not yet tried it on visual studio or xcode. so thanks for the corection!!!

This post has been edited by Jingle: 31 December 2007 - 05:39 PM

Was This Post Helpful? 0
  • +
  • -

#7 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 45
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Edit text file using c++

Posted 31 December 2007 - 05:51 PM

No problem. If you try out a newer compiler (gcc 3.4.2 is my staple compiler), you'll probably see warnings about this if you use fstream.h (same for iostream.h and several others). If it's what works for your compiler, go for it. But if at all possible, update that compiler as soon as you get a chance :)

This is obviously tough for courses where software is provided, but you may want to upgrade on your home machine, for instance. But don't do anything that'll get you docked marks - I wouldn't want to be responsible for that ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1