Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to overwrite information inside file local system? C/C++

Status
Not open for further replies.

zenniz

Junior Member level 3
Junior Member level 3
Joined
Mar 25, 2013
Messages
29
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Singapore
Activity points
1,497
Still a beginner here.
I have a txt file with all the value in it (Values will not exceed 10000)

Code:
//Information inside values.txt
1234
12
345
6789

How to for example overwrite the first sentence (1234) to 3210 so that the end result will be

Code:
//Information inside values.txt
3210
12
345
6789
 

Changing 1234 to 3210 is very easy, but how about changing 12 to 1235?

Before thinking about programming details, you'll need to consider file structures and what you exactly want to do.

I presume that each line is terminated with <CR><LF> without hidden spaces. Then you can easily replace a line with a different one of the same length. But to change the length, you'll need to copy and rewrite the whole file. Consider if this apprpriate or if you should use a different file structure.
 

It is easy

you would charge file to RAM (array, count the line of file for array size), after you change the value, you would write the file again.

Code:
int line_count(FILE *fp)
{
	int count=0;
	char letra;
	letra=getc(fp);
	while(!feof(fp))
	{
		if(letra=='\n')
		{
			count++;
		}
		letra=getc(fp);
	}
	rewind(fp);
	return count;
}


enjoy
 
Last edited:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top