program wont output line to file
Apr 12, 2009 at 9:44pm UTC
Hey it's me again,
Everything executes flawlessly save for outputting new users to a .dat file
It runs fine all the way through executing the "cout << "New User added" line but doesnt write any data to a file
I've checked permissions on the file and all should be well....also the file is in the same folder as the source code.
Here is the code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
#include "users.h" //user class
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <strstream>
using namespace std;
//================Global Variables==========//
user current_user;
user new_user;
fstream users;
//================Erike menu===============//
int menu(void )
{
system("PAUSE" );
return (0);
}
//===============return user===============//
int return_user(void )
{
cout << "Welcome " ;
cout << current_user.fname;
menu();
}
//==============New User===================//
int new_users(void )
{
users.open("users.dat" , ios::out | ios::ate);
if (users.is_open())
{
//====================================== //
users << "new_user: " ; // This is the code that is not executing //
users << current_user.fname; // //
users << "\n " ; // //
//====================================== //
cout << "New User added \n" ;
menu();
} else cout << "files error" ;
users.close();
}
//==============Welcome screen=============//
int welcome(void )
{
string line;
users.open("users.dat" ,ios::in);
//open users.ini for background check
cout << "Hiya! My name is Erika, who are you?" ;
cin >> current_user.fname;
if (users.is_open())
{
for (int J = 1; J <= 32;)
{
getline(users, line, ' ' );
if (line == current_user.fname)
{
return_user();
} else J = (J + 1);
if (J == 32)
{
users.close();
new_users();
}
}
}else cout << "file error" ;
}
//==============Main=====================//
int main(int nNumberofArgs, char * szArs[])
{
welcome();
system("PAUSE" );
return 0;
}
I just want it to output those three lines to the users.dat file
As a note, I'd like to add that I've tried the same code with a .ini, and .txt
Topic archived. No new replies allowed.