954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Difficulty with IO

Hi, I need help with something I'm working on. I just want to get a basis to start, but I'm hoping to end up with a system where I can store encrypted output in an outside file. Right now my algorithms are a little elementary to consider even stable, but I'm just working on this one program. :cheesy: The objective is to take a persons input (username=uname) and (password=pword), take them and convert them to int and output them in a file. The next step is to open that file, read it line by line, store it in an array, and display the letter the int stands for (example file out would be 98, thus translated back it would be 'a'). Here's what I have so far.

#include
#include
#include
#include
using namespace std;

int main(void)
{
char uname[30];
char pword[30];
char fname[34];
char q;
int x;
int y;
char *stuff=okay;
char okay[30];
ofstream myfile;
ifstream file2;
z=0;
cout << "Please enter your username: ";
cin >> uname;
myfile.open("temp.txt", ios:: out);
myfile << uname << ".txt";
myfile.close();
cout << "\nPlease enter your password: ";
cin >> pword;
file2.open("temp.txt", ios::in);
file2 >> fname;
file2.close();
myfile.open(fname, ios::ate);
for (x=0; x < strlen(pword); x++)
{
q = pword[x];
y = int(q);
cout << "\n" << y;

myfile << y << "\n";
}
myfile.close();
for (x=0; x

kenjutsuka
Light Poster
45 posts since Oct 2006
Reputation Points: 22
Solved Threads: 1
 

Two points about ur program:

1. Dont flush the input stream because it has got undefined behaviour.

2. YOu are assigning the char array "okay" to char pointer stuff but till that point okay has not been defined. Swap the two stmts to avoid the syntax error.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 
for (x=0; x <strlen(pword); x++)
{
file2.open(fname, ios::in);
file2.getline(stuff, 3);
file2.close();
cout << (char)okay;
}


What the H is the above supposed to do? What itIS doing is opening a file, reading the first line, the closing the file -- all that possibly hundreds of times (depending on length of pword). Why in the world would you want to waste so much CPU time doing the same identican thing so many times?

You are obviously writing a c++ program. Then why are you using C style character arrays instead of c++ std::string class? There are valid reasons for doing it, but I don't see any valid reasons in your program, unless of course (1) your instructore required it, or (2) you have not learned about std::string yet.

std::string uname;
std::string pword;
std::string fname;
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
for (x=0; x <strlen(pword); x++)
{
file2.open(fname, ios::in);
file2.getline(stuff, 3);
file2.close();
cout << (char)okay;
}

What the H is the above supposed to do? What itIS doing is opening a file, reading the first line, the closing the file -- all that possibly hundreds of times (depending on length of pword). Why in the world would you want to waste so much CPU time doing the same identican thing so many times?

You are obviously writing a c++ program. Then why are you using C style character arrays instead of c++ std::string class? There are valid reasons for doing it, but I don't see any valid reasons in your program, unless of course (1) your instructore required it, or (2) you have not learned about std::string yet.

std::string uname;
std::string pword;
std::string fname;

I am using char arrays, because in the file i/o procedures, char arrays are required, and because I am typecasting them from char to int and you cannot typecast std::string, at least not in VC.NET. And a fix to the above mention code I did was

file2.open(fname, ios::in);
for (x=0; x <strlen(pword); x++)
{
file2.getline(stuff, 3);
cout << (char)okay;
}
file2.close();
kenjutsuka
Light Poster
45 posts since Oct 2006
Reputation Points: 22
Solved Threads: 1
 

Could you give me and example about how to switch the stmts? And the fflush was only there for my convenience, but I got rid of it.

kenjutsuka
Light Poster
45 posts since Oct 2006
Reputation Points: 22
Solved Threads: 1
 
I am using char arrays, because in the file i/o procedures, char arrays are required, and because I am typecasting them from char to int and you cannot typecast std::string, at least not in VC.NET.

depends. please post an example of what you mean -- there may be other better or more appropriate alternatives.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
depends. please post an example of what you mean -- there may be other better or more appropriate alternatives.



For instance

char y;
 
cin >> y;
 
cout << (int)y;


However with:

std::string y;
 
cin >> y;
 
cout << (int)y;

You would get a compile error saying "cannot convert char to std::string" on the 3rd line.

kenjutsuka
Light Poster
45 posts since Oct 2006
Reputation Points: 22
Solved Threads: 1
 

In the first chase y is just one single character, in the second case it is a string. This is not an example of what I was thinking about and you would be better off leaving it the way you have it. Something like below is what I was thinking

char buf[255];

in.read(buf,sizeof(int));

int x = *(int*)buf;


above could be rewritten like this, but I'm sure you probably already know that.

int x;
in.read(&x,sizeof(int);
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You