| | |
Difficulty with IO
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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.
#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.
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.
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.
C++ Syntax (Toggle Plain Text)
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 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)
std::string uname; std::string pword; std::string fname;
Last edited by Ancient Dragon; Oct 4th, 2006 at 6:53 am.
•
•
•
•
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?C++ Syntax (Toggle Plain Text)
for (x=0; x <strlen(pword); x++) { file2.open(fname, ios::in); file2.getline(stuff, 3); file2.close(); cout << (char)okay; }
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)
std::string uname; std::string pword; std::string fname;
C++ Syntax (Toggle Plain Text)
file2.open(fname, ios::in); for (x=0; x <strlen(pword); x++) { file2.getline(stuff, 3); cout << (char)okay; } file2.close();
Last edited by kenjutsuka; Oct 4th, 2006 at 8:29 am.
depends. please post an example of what you mean -- there may be other better or more appropriate alternatives.
•
•
•
•
depends. please post an example of what you mean -- there may be other better or more appropriate alternatives.
C++ Syntax (Toggle Plain Text)
char y; cin >> y; cout << (int)y;
However with:
C++ Syntax (Toggle Plain Text)
std::string y; cin >> y; cout << (int)y;
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
above could be rewritten like this, but I'm sure you probably already know that.
C++ Syntax (Toggle Plain Text)
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.
C++ Syntax (Toggle Plain Text)
int x; in.read(&x,sizeof(int);
![]() |
Similar Threads
- difficulty creating a formula with a macro (Visual Basic 4 / 5 / 6)
- Difficulty understanding Object Orientated File Streaming (C++)
- My Daddy (Geeks' Lounge)
- Difficulty viewing Media programs (Windows NT / 2000 / XP)
- Why won't IE work properly? (Web Browsers)
Other Threads in the C++ Forum
- Previous Thread: Linker error in Database connectivity..
- Next Thread: Multithreading in C++
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






