Skip to main content
gamedev.net
🔒 Locked

Adding lines to files.

Started by KurtCPP Jan 28, 2004 at 7:07 AM 7 replies 1.3k views
Original Post
KurtCPP
KurtCPP
Hi every-1. Could someone shed light on this please? I intend to make a program that adds lines to a file, at the beginning, for the moment. The problem is I don't know whether I really must copy the whole file's content in order to copy it back behind the line added. It would be something like :
void AddLine(char *str, char *filename){
  int n;
  char *buffer = null;
  fstream fstr(filename,ios::in|ios::out);
  fstr.seekg(0,ios::end);
  n = fstr.tellg();
  n += strlen(str);
  n += 1;                    // for the null character

  buffer = new char[n];
  strcpy(buffer,str);
  fstr.seekg(0, ios::beg);
  fstr.read(buffer+strlen(str));
  buffer[n] = 0;

/*for those who don't like reading, it's just something that copies the
 whole file in abuffer after having copied into this same buffer the string
 to add at the beginning. Then rewrite everything to the same file and
 save it.
Some instructions are missing here but don't report me them, they're already corrected in the final version. ;-)
This will then make a pretty cool Linux command : add MyAss
 AnyFile.txt*/
Prog, Hex & Rock'n'Roll : I don't like the Prog but the Prog likes me. Some nice poetry to sweeten your spirit and relax a bit before programming [edited by - kurtcpp on January 28, 2004 8:09:07 AM] [edited by - kurtcpp on January 28, 2004 8:11:34 AM]
Prog, Games & Rock'n'Roll :I don't like the Prog but the Prog likes me.
Nice Coder
Nice Coder
try
open file for append as #1
print #1, string
close #1


If you get stuck, try opening the file, reading it into a string, then open the file and print the string + what you wanted at the end of it.

Nice coder
Click here to patch the mozilla IDN exploit, or click Here then type in Network.enableidn and set its value to false. Restart the browser for the patches to work.
Fruny
Fruny
No, just seekg() to the end and start writing there.
Alternatively, open with ios::in|ios::out|ios::app.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan (C programming language co-inventor)
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
KurtCPP
KurtCPP
Thank you for your help. I had tried the method consisting of reading the whole file and copying it in a buffer after the line to add at the beginning. I already thought it''d have worked but my question was rather : ain"t there a faster and more adapted way??
For those who are interested, here''s the code :

void AddLine(char *str, char *filename){
int n, filesize;
char *buffer = null, *line = null;
fstream fstr(filename,ios::in|ios::out);

line = new char[strlen(str)+2];
strcpy(line,str);
strcat(line,"\n\0");

fstr.seekg(0,ios::end);
n = (filesize = fstr.tellg());
n += strlen(line);
n += 1; // for the null character

buffer = new char[n];
buffer[n] = 0;
strcpy(buffer,line);
fstr.seekg(0,ios::beg);
fstr.read(buffer+strlen(str)+1,filesize);
fstr.seekp(0, ios::beg);
fstr.write(buffer,n-1);
fstr.close();
}


Prog, Hex & Rock''n''Roll :
I don''t like the Prog but the Prog likes me.
Some nice poetry to sweeten your spirit and relax a bit before programming
Prog, Games & Rock'n'Roll :I don't like the Prog but the Prog likes me.
Fruny
Fruny
You didn''t read my post, did you ?

void AddLine(char *str, char *filename){
fstream fstr(filename,ios::in|ios::out|ios::app);
fstr << str << endl;
}


Ought to do the job.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan (C programming language co-inventor)
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quirk
Quirk
I think he wants to add a line at the beginning of the file, not append one at the end.

/Q
Fruny
Fruny
Ok, my bad.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan (C programming language co-inventor)
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
JOL
JOL
I would probably do it like this, might not be best in all situations but for small files, simple to implement too.

1. Create a new file - tempfile
2. Write your data to the beginning of that file
3. copy the old file to the end of the new file
4. close files and rename the tempfile to the oldfiles name ...
5. done!
6. have a cup of coffe
7. tap myself on the shoulder for a good days work
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~I'm looking for work

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.