Needing a little help - Problem with Files

Write here if you have problems with your C source code

Moderator: Board moderators

Needing a little help - Problem with Files

Postby leoisl » Fri Feb 09, 2007 1:26 pm

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 :-?
leoisl
New poster
 
Posts: 3
Joined: Mon Dec 04, 2006 8:36 am

Re: Needing a little help - Problem with Files

Postby lnr » Tue Oct 07, 2008 5:17 pm

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.
User avatar
lnr
Experienced poster
 
Posts: 134
Joined: Sat Jun 30, 2007 2:52 pm
Location: (DU,CSE)Dhaka,Bangladesh

Re: Needing a little help - Problem with Files

Postby mf » Tue Oct 07, 2008 5:54 pm

Your code doesn't seem to do what the op wanted.

Resizing an existing file is what ftruncate()/truncate() do.
mf
Guru
 
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland


Return to C

Who is online

Users browsing this forum: No registered users and 1 guest