Difficulty with IO

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2006
Posts: 45
Reputation: kenjutsuka is an unknown quantity at this point 
Solved Threads: 1
kenjutsuka's Avatar
kenjutsuka kenjutsuka is offline Offline
Light Poster

Difficulty with IO

 
0
  #1
Oct 4th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Difficulty with IO

 
0
  #2
Oct 4th, 2006
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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Difficulty with IO

 
0
  #3
Oct 4th, 2006
  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.

  1. std::string uname;
  2. std::string pword;
  3. std::string fname;
Last edited by Ancient Dragon; Oct 4th, 2006 at 6:53 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 45
Reputation: kenjutsuka is an unknown quantity at this point 
Solved Threads: 1
kenjutsuka's Avatar
kenjutsuka kenjutsuka is offline Offline
Light Poster

Re: Difficulty with IO

 
0
  #4
Oct 4th, 2006
Originally Posted by Ancient Dragon View Post
  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.

  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
  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 45
Reputation: kenjutsuka is an unknown quantity at this point 
Solved Threads: 1
kenjutsuka's Avatar
kenjutsuka kenjutsuka is offline Offline
Light Poster

Re: Difficulty with IO

 
0
  #5
Oct 4th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Difficulty with IO

 
0
  #6
Oct 4th, 2006
Originally Posted by kenjutsuka View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 45
Reputation: kenjutsuka is an unknown quantity at this point 
Solved Threads: 1
kenjutsuka's Avatar
kenjutsuka kenjutsuka is offline Offline
Light Poster

Re: Difficulty with IO

 
0
  #7
Oct 4th, 2006
Originally Posted by Ancient Dragon View Post
depends. please post an example of what you mean -- there may be other better or more appropriate alternatives.
For instance

  1. char y;
  2.  
  3. cin >> y;
  4.  
  5. cout << (int)y;

However with:
  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Difficulty with IO

 
0
  #8
Oct 4th, 2006
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
  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.
  1. int x;
  2. in.read(&x,sizeof(int);
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC