• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Quick Simple C++ Question

I am trying to read a file in C++ and take each of the Characters and increase them by 2 characters (For instance A would be C).

I can get the file to read, but seeing as I haven't done this in 4 years, I can't seem to get the code to work that reads the characters and increases them and then outputs them to their very own file.

Thank You so much,
-Kevin

Edit: I have no idea what is wrong with the code window. It is going haywire-- if anyone can read what I have typed, that would be great though. I can't put it in a window, but hopefully this will help

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

int main()
{
string filename;
char Characters;
int X;

ifstream Original_Doc;
cout << "Please enter the file name\n";
cin >> filename;
Original_Doc.open(filename.c_str());
if (!Original_Doc)
{
cout << "Unable to open file Original_Doc";
exit (1);
}

else
{
Original_Doc >> Characters;

for (X=1; X<= Characters; X++)
{
Original_Doc >> X;
cout << X;
}
}

return 0;
}
 
I would not read one character at a time from the file. Read at least a line. I define a line, in a text file, as all characters up to and including the end-of-line \n character.

Save that into a txt buffer and iterate through that, adding 2 to each, like so (in pseudo-code):

while (!feof( fp ))
{


while ( not end-of-file )
{
read_line(from_file, txtbuf)
{
txtbuf[i++] += 2;
}
}

This saves in place, but if you want, you could easily save to a separate txt buffer.

As always, the following caveat applies:

If this will run in a secure environ, make sure the buffer is large enough or dynamic enough to avoid overflows.

Also, to avoid corrupting the original file, I would not save in place. Make a tmp file, same to that, rename at the end of a successful conversion.

 
Well I actually got it to read all characters including the spaces with my code like this. This is a very basic C++ class so I don't want to go past what this chapter covers as far as reading an entire line (They are very very picky)

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

int main()
{
string filename;
char Characters;

ifstream Original_Doc;
cout << "Please enter the file name\n";
cin >> filename;
Original_Doc.open(filename.c_str());
if (!Original_Doc)
{
cout << "Unable to open file Original_Doc";
exit (1);
}

else
{
for (int X=1; !Original_Doc.eof(); X++)
{
Original_Doc >> noskipws >> Characters;
cout << Characters;
}
}

return 0;
}

The only thing is I can't seem to find the code that increases characters by a specified integer. Can anyone tell me what function call that is?

-Kevin
 
It's simple addition, not requiring a function. An individual element of a string is an integer in the range of 0-255, or -128 to 127 for signed. The fact that it holds a human readable character is simply that it holds a number that falls within a range defined to the computer as representing a series of 1s and 0s that define a character what we've come to associate as an 'A' or 'g'.

 
Code... data... text characters... sounds... textures... vertices... all 1s and 0s. What they actually represent is only of consequence to the device that is interpreting those 1's and 0's be it the DAC of the sound card or a VGA text mode BIOS. You can make an assembly dump out of a .jpg file and have valid instructions, valid ASCII text, etc. Doesn't mean it's going to be orderly and useful though. In the end, bytes are bytes, and can all be handled the same way.

So...

char a = 'A';
a+=2;

then:

a == 'C'

So in pseudo code, something like:

while ((c = getchar(input)) != EOF)
{
if ( (c >= 'A' && c <='Z') || (c >= 'a' && c <='z') )
{
c +=2;
}
putchar(c, output);
}

There shouldn't be a penalty for reading and writing to files one character at a time. Any modern OS is going to perform block reads and writes to and from file stream buffers. These buffers are going to be in process memory space so even API overhead is minimized (no kernel switch every time you call getch() unless the buffer needs to be refreshed which will happen 512 bytes or so at a time if not 4k at a time).
 
You would think there wouldn't be a penalty, but they got mad and marked me down for including a simply loop when we hadn't covered it yet.

The problem was my own stupidity. I accidentally forgot to remove the line that output the original text. So the original text was in there. Also I had it reading spaces and increasing the space is impossible (Well they only want me to change alphanumeric text).

All I need to do is output this stupid thing to a text file and I'm done. Thanks for the help guys-- I feel stupid because it seemed that every problem I ran into was a result of me not paying attention.

-Kevin
 
Originally posted by: exdeath
There shouldn't be a penalty for reading and writing to files one character at a time. Any modern OS is going to perform block reads and writes to and from file stream buffers.

Sounds as if your working off of faith there. Modern OSs should do block reads, though I've not discounted that, I find it better, logically, to work with lines.

I find it hazardous to rely on someone else to comply to my wishes.
 
Originally posted by: Gamingphreek
You would think there wouldn't be a penalty, but they got mad and marked me down for including a simply loop when we hadn't covered it yet.

In the future, when asking homework questions, state so...and you'll want to list the conditions under which you must work. I doubt anyone will give you the answer, but we can tell you how to think.

Regardless, I think it's kind of odd they would want you to alter a file within such constraints. Sounds as if they have too strict a definition of how it should be.
 
Originally posted by: aCynic2
Originally posted by: Gamingphreek
You would think there wouldn't be a penalty, but they got mad and marked me down for including a simply loop when we hadn't covered it yet.

In the future, when asking homework questions, state so...and you'll want to list the conditions under which you must work. I doubt anyone will give you the answer, but we can tell you how to think.

Regardless, I think it's kind of odd they would want you to alter a file within such constraints. Sounds as if they have too strict a definition of how it should be.

Yea-- I should have stated that this was homework. I don't want people to flat out give me code-- just a nudge in the right direction-- I am sorry if my question was misleading in any way shape or form, I didn't mean for it to be that way.

They are very very strict. I got a 90 instead of a 100 on my test because, they didn't state this, I didn't include a .0 on all my doubles. So I had double X=10, I got 10 points off because it wasn't X=10.0. I was absolutely ticked.

I have a 100 on all the other grades though so I just got over it.

-Kevin
 
Originally posted by: Common Courtesy
What are you going to display when you encounter the characters Y and Z?

I asked the TA that and he said for the purposes of this class he wants 'y' to go to 'A' and 'z' to go to 'B'. So I just made a quick else if statement threw it in and didn't think anymore of it.

I really wish they would allow some freedom in programing. Obviously, if this were a text file which actually had a 'A' then we would have a problem decrypting it. I just wish the class was more practical (While I'm wishing for things, I wish I could have tested out of the class-- I haven't been to a single lecture, and I go to lab turn in my work and leave)

-Kevin
 
Originally posted by: aCynic2
Originally posted by: exdeath
There shouldn't be a penalty for reading and writing to files one character at a time. Any modern OS is going to perform block reads and writes to and from file stream buffers.

Sounds as if your working off of faith there. Modern OSs should do block reads, though I've not discounted that, I find it better, logically, to work with lines.

I find it hazardous to rely on someone else to comply to my wishes.

Yeah, me too, but I also like to avoid redundant work also. 😉
 
Here you go, using STL to increment all characters. Although if your TA marked you down for using a loop then they will probably give you a zero for this.

Edit: What's up with the attach code feature?
 
Here is a way that is little more fun. Because ASCII is 7 bits and the 6th bit is the key to (upper/lower) case then we can logically make the math work too.
First we bump by 2.
Then check for roll over in which case AND off the MSBit then exclusive (XOR) to flips case. Next we will have rolled into the brackets, slashes and bars so the OR operation with 0x20 in the if statement forces lower case and we check for greater than 'z' then subtract 26 to wrap back into the 26 valid letters whether its upper or lower.

This simplifies the if statement with one condition and allows the CPU to parallel instructions.
Code:
char c1=0

Original_Doc >> c1;
char c2 = c1 + 2;
if( (c2 | 0x20) > 'z' )
{
   c2 = ( c2 & 0x7f) ^ 0x20;  
   c2 -= 26;
}
cout << c2;
 
Back
Top