text input/ouput + C++?

2009-08-24 2:03 pm
Write a program to insert a blank line after each line in a file.

anyone can give me some hint on doing this question?
thnx a lot!

回答 (4)

2009-08-24 3:23 pm
✔ 最佳答案
Here's the pseudo code:

get input file name from user
open file
if input file openeed successfully
..get output file name from user
..if output file opened successfully
....while not eof (input file)
......read line from input file to buffer
......write line from buffer to output file
......write additional newline to output file
....end while
....close output file
..end if
..close input file
endif
-- optional extra stuff
rename input file (e.g. rename to original_name.bak)
rename output file to original name of input file

Now all you need to do is code this up in C++.
2016-12-17 5:24 am
So authentic... If i'm enjoying C.O.D. my female pal the two extra appropriate be twiddling with me, or be arranged to be left out. She knows this nevertheless. If i do no longer answer she asks me what interest i grow to be enjoying. i did no longer have a clean 3 hundred and sixty 5 days's decision so i think i did no longer shop it?
2009-08-24 2:13 pm
you would use fopen to open the file (http://www.cplusplus.com/reference/clibrary/cstdio/fopen/)

You will want to read in the entire file into a buffer. If you use fseek to go to the end, and then ftell. You can find out the size of the file and thus the buffer to allocate (http://www.cplusplus.com/reference/clibrary/cstdio/fseek/,
http://www.cplusplus.com/reference/clibrary/cstdio/ftell/).

Allocate a buffer based on the value you get from ftell and read the file in using fread (http://www.cplusplus.com/reference/clibrary/cstdio/fread/).

Make sure you use ftell to return the pointer to the beginning after using ftell and the reading in the file.

Now, search through the buffer for the \n character which denotes end of line.Write out data from after the last \n to the current \n to write the current data back out. Then write out a endl to insert the blank line.
2009-08-24 2:09 pm
char someText[30];
ifstream fin;

cout << "Enter the file name to save the game to: ";
fin >> someText >> endl >> endl;


收錄日期: 2021-04-25 13:28:15
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20090824060324AA93Evb

檢視 Wayback Machine 備份