Page 1 of 1

Needing a little help - Problem with Files

Posted: Fri Feb 09, 2007 1:26 pm
by leoisl
Hi All,

I searched through google but couldn't find an answer. My doubt is how to put the EOF mark in any place of a file. What I'm supposing seems like to put a NULL mark ('\0') in the middle of a string in order to "eliminate" all the others characters after it.

Example:
You have these data on a input file:

Code: Select all

1 2 3 4 5 6 7 8 9 10 <EOF>
Now, I have to make a program to eliminate the last number and save the output in the same file as the input. In other words, if I use the above input file in my program, the input file after it ran should be:

Code: Select all

1 2 3 4 5 6 7 8 9 <EOF>
Obviously, I eliminated the last number (10).

The real problem is that I wanted just to move the EOF mark before the number 10, so I eliminate it. This would be the output of my program

Code: Select all

1 2 3 4 5 6 7 8 9 <EOF> 10
which in my mind would finally result in

Code: Select all

1 2 3 4 5 6 7 8 9 <EOF>
The problem of eliminating the last number was just an example. What I really want to know is if I can move the EOF mark to any place in a file.[/code]

thanks in advance and forgive me if I did any english mistake :-?

Re: Needing a little help - Problem with Files

Posted: Tue Oct 07, 2008 5:17 pm
by lnr
To leoisl

EOF is always at the last.

If the (m.txt )file content is like this

Code: Select all

4
6
7
567 3 3 5 




7 
7 8 3


5
Running this code:

Code: Select all

#include <iostream>
#include <string>
using namespace std;

int main()
{
	freopen("m.txt","r",stdin);
	char ch;
	int i,c;
	c=0;
	while(cin>>i)
	{
		++c;
		cout<<i<<' ';
		if(c%20==0)
			cout<<endl;
	}
	cout<<endl;
	return 0;
}
Output:

Code: Select all

4 6 7 567 3 3 5 7 7 8 3 5 
Thanks all.

Re: Needing a little help - Problem with Files

Posted: Tue Oct 07, 2008 5:54 pm
by mf
Your code doesn't seem to do what the op wanted.

Resizing an existing file is what ftruncate()/truncate() do.