How to Make Program Read Inuts From File
Writing a programme to read an input file
Y'all accept to write a programme to read an input file graphic symbol by character to assistance Peter solve the post-obit activity in his activity book. The paragraph below is given:
Nosotros h2pe that 32u e5723ed the acti4it3. A6ter 32u ha4e c2mpleted the acti4it3, 0e5d 32ur re0ult t2: The Acti4it3 C2mpetiti25, Bett3 Da4i0 0treet 99, Auckla5d Park, 989, a5d 0ta5d a cha5ce t2 wi5 a hamper c250i0ti51 26 c2l2uri51 a5d acti4it3 b22k0, c2l2uri51 pe5cil0 a5d pe50.
Create an input file activity.dat with the paragraph above. The numbers 0 to 7 have to exist replaced as follows:
0 must be replaced by south
1 must be replaced by one thousand
2 must be replaced by o
iii must exist replaced by y
four must be replaced by five
5 must exist replaced by due north
6 must be replaced by f
seven must be replaced past j
Inquire the user to input the names of the input and output files. Read the input file character by grapheme, and write the character (if it stays the same) to the output file, or write the inverse character to the output file. Call your output file competition.txt
So,if im intepreting this right , I have to brand a txt.file and re-create paste the paragraph , then write a plan to read that txt.file and then output it to some other txt.file with the replacements?
If then, can someone just assist with how you actually can do that? DON"T WRITE THE WHOLE CODE. but explain to me how y'all actually are able to let it read it and convert it?
Currently using code_blocks and doing it all via console
> so write a plan to read that txt.file then output it to some other txt.file with the replacements?
Yes.
As a useful offset exercise, merely see if yous can brand an authentic re-create of the input file without any substitutions.
| |
The next step is brand transform_char deal with just one of the substitutions.
You re-test and make certain your output file is heading in the right direction.
And so,if im intepreting this right , I have to make a txt.file and copy paste the paragraph , then write a plan to read that txt.file and then output it to another txt.file with the replacements?
This is more or less right and shows your plan/framework (pseudocode even), maybe change a few things and aggrandize on it equally follows:
ane. Make a .txt file ( call information technology some_text.txt ) 2. Manually re-create paste the paragraph into the file 3. Create a new file for output (output.txt) iv. Write a program to read that .txt file character by character 5a. --- Process each character by making replacements 5b. Save processed character to output.txt file 5c. --- Loop until there are no more characters 6. All done and then close files.
make the text file in notepad to first: the consignment says to read the file, but not where it came from, so it is user provided non code generated.
at present write code to open the file, read it in letter of the alphabet by letter (their pick, they told you lot do do it this manner) and perform the lookup and exchange.
by the style, bank check this out:
unsigned char tbl[256];
std::iota(tbl, tbl+ 256, 0); //this is just filling the tabular array with 0-255 which you can exercise in a loop if you lot want.
tbl['0'] = 'south'; //0 and '0' are not the same. it must exist in quotes here
tbl['i'] = 'g';
... fill in the remainder of these...
the output you want is (ofs is ofstream ifs is ifstream variable proper noun examples)
ofs << tbl[inputletter];
so basically with the to a higher place table all you need is:
ifs >> letter;
ofs << tbl[letter of the alphabet]; //when letter == '0' y'all become south, because of the table conversion.
there are examples in the reference section on this site on how to practise the two files. they work a lot like cin and cout:
ifstream ifs(infilename); //opens infilename, simply you demand to check to see if it worked (that is, handle a bad file proper noun from user)
ifs >> letter; //works similar cin
ofstream ofs(outfilename); //you create or over-write this one, no demand to check however it can fail if user said to make information technology in a read only place or nonexistent drive letter etc..! You can probably skip that for your homework, but know that you do need to check it in a serious program
ofs << letter; //works like cout
you tin can also check each letter with if statements or a switch statement to manually convert information technology.
if(letter == '0') .. do something
else if(letter == '1')
else if ... //over and over
else default activity ...
Concluding edited on
You can also employ 2 strings - before and later. If find read char in earlier string and then supplant with later char std::string.discover()
| |
ifs >> letter;
ignores white space unless you also use std::noskipws
Final edited on
| |
Here is my reading code so far, it worked , it displayed the words on the console.
I am now trying to figure out how to let that string work you gave me @seeplus
Last edited on
@OP
Information technology's possible to apply <vector>'s and <string>'s only given you starting post it'southward not necessary. You're simply reading a character, which is all y'all will do with <string> functionality and the <vector> would just exist useful if you wanted to shop the data in retention for some (unknown?) reason.
The challenge is to come up up with a way of efficiently performing the replacements and, aside from other possibilities, a couple of possibilities have already been mentioned - switches and if'south - at that place are other means too with mapping the characters.
| |
Terminal edited on
to figure out how to let that string work yous gave me @seeplus
Maybe:
| |
Terminal edited on
| |
@OP
I misread the significance of <string>'s in the earlier post thinking information technology meant reading the file into a string. I of the few occasions in my life where I accept to take dorsum a comment for being wrong. All the same that's some other manner of replacing the chars., and is one example of a mapping instead of a pile of if's or a switch which don't lend themselves to re-usability.
As you lot tin encounter none of these require <vector>'south
As per my previous comment, this ignores white space chars unless you also use std::noskipws
that is why I wasted the space for a total ascii table. If you go my route, it is but
| |
Final edited on
I concur this is faster :)
| |
I don't similar many assignments! :)
@OP
Lot'south of options and lots of choices to make now.
0p2ilt 62r ch2ice
Another way to translate the characters is a switch statement. This might exist clearer to a beginner, merely jonin's lookup table is probably faster.
| |
No surprises with a <map> option:
| |
ty all very much , I actually learned quite a lot today
my understanding is that most of the time, unless prevented past something, switches ARE lookup tables when compile. Its probably nigh the same, and in turn, the map is too pretty close to what I did.
Concluding edited on
Topic archived. No new replies allowed.
Source: https://www.cplusplus.com/forum/beginner/278128/
Post a Comment for "How to Make Program Read Inuts From File"