943,937 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2038
  • C++ RSS
Oct 4th, 2006
0

Difficulty with IO

Expand Post »
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 <iostream>
#include <fstream>
#include <iomanip>
#include <string>
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 <strlen(pword); x++)
{
file2.open(fname, ios::in);
file2.getline(stuff, 3);
file2.close();
cout << (char)okay;
}


fflush(stdin);
getchar();
return(0);
}

Help is appreciated, thanks.
Similar Threads
Reputation Points: 22
Solved Threads: 1
Light Poster
kenjutsuka is offline Offline
45 posts
since Oct 2006
Oct 4th, 2006
0

Re: Difficulty with IO

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.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Oct 4th, 2006
0

Re: Difficulty with IO

C++ Syntax (Toggle Plain Text)
  1. for (x=0; x <strlen(pword); x++)
  2. {
  3. file2.open(fname, ios::in);
  4. file2.getline(stuff, 3);
  5. file2.close();
  6. cout << (char)okay;
  7. }

What the H is the above supposed to do? What it IS 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.

C++ Syntax (Toggle Plain Text)
  1. std::string uname;
  2. std::string pword;
  3. std::string fname;
Last edited by Ancient Dragon; Oct 4th, 2006 at 6:53 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Oct 4th, 2006
0

Re: Difficulty with IO

C++ Syntax (Toggle Plain Text)
  1. for (x=0; x <strlen(pword); x++)
  2. {
  3. file2.open(fname, ios::in);
  4. file2.getline(stuff, 3);
  5. file2.close();
  6. cout << (char)okay;
  7. }
What the H is the above supposed to do? What it IS 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.

C++ Syntax (Toggle Plain Text)
  1. std::string uname;
  2. std::string pword;
  3. 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
C++ Syntax (Toggle Plain Text)
  1. file2.open(fname, ios::in);
  2. for (x=0; x <strlen(pword); x++)
  3. {
  4. file2.getline(stuff, 3);
  5. cout << (char)okay;
  6. }
  7. file2.close();
Last edited by kenjutsuka; Oct 4th, 2006 at 8:29 am.
Reputation Points: 22
Solved Threads: 1
Light Poster
kenjutsuka is offline Offline
45 posts
since Oct 2006
Oct 4th, 2006
0

Re: Difficulty with IO

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.
Reputation Points: 22
Solved Threads: 1
Light Poster
kenjutsuka is offline Offline
45 posts
since Oct 2006
Oct 4th, 2006
0

Re: Difficulty with IO

Click to Expand / Collapse  Quote originally posted by kenjutsuka ...
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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Oct 4th, 2006
0

Re: Difficulty with IO

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

C++ Syntax (Toggle Plain Text)
  1. char y;
  2.  
  3. cin >> y;
  4.  
  5. cout << (int)y;

However with:
C++ Syntax (Toggle Plain Text)
  1. std::string y;
  2.  
  3. cin >> y;
  4.  
  5. cout << (int)y;
You would get a compile error saying "cannot convert char to std::string" on the 3rd line.
Reputation Points: 22
Solved Threads: 1
Light Poster
kenjutsuka is offline Offline
45 posts
since Oct 2006
Oct 4th, 2006
0

Re: Difficulty with IO

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
C++ Syntax (Toggle Plain Text)
  1. char buf[255];
  2.  
  3. in.read(buf,sizeof(int));
  4.  
  5. int x = *(int*)buf;

above could be rewritten like this, but I'm sure you probably already know that.
C++ Syntax (Toggle Plain Text)
  1. int x;
  2. in.read(&x,sizeof(int);
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Linker error in Database connectivity..
Next Thread in C++ Forum Timeline: Multithreading in C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC