Trying to return a created .txt file
May 13, 2015 at 10:45pm UTC
Hi. I'm having trouble returning a .txt file that is being created and cant seem to get it to work. It will create it, but once i try to select it, it will: read it, delete it, and then recreates it.
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
void f1()
{
cout << "\nName of data file: " ;
cin >> filename;
inputFile.open("filename.txt" );
if (inputFile)
{
while (inputFile >> number)
{
cout << number << endl;
cout << "File " << filename << " selected." ;
}
inputFile.close();
}
else (outputFile);
{
cout << "\nFile not found, creating file\n\n" ;
outputFile.open("filename.txt" );
outputFile.close();
}
system("pause" );
return filename;
}
Here is the main:
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
int main()
{
int choice;
string filename;
a = rand() % 10 + 1;
b = rand() % 10 + 1;
c = rand() % 10 + 1;
d = rand() % 10 + 1;
e = rand() % 10 + 1;
f = rand() % 10 + 1;
g = rand() % 10 + 1;
h = rand() % 10 + 1;
i = rand() % 10 + 1;
j = rand() % 10 + 1;
srand(time(0));
const int choice1 = 1,
choice2 = 2,
choice3 = 3,
choice4 = 4,
choice5 = 5,
choice6 = 6,
choice7 = 7;
do
{
showmenu();
cout << "Menu Choice: " ;
cin >> choice;
while (choice < choice1 || choice > choice7)
{
cout << "Menu Choice: " ;
cin >> choice;
}
if (choice != choice7)
{
switch (choice)
{
case choice1:
f1();
break ;
case choice2:
f2();
break ;
case choice6:
f6();
break ;
}
}
}while (choice != choice7);
system("pause" );
return 0;
}
Last edited on May 13, 2015 at 10:48pm UTC
May 14, 2015 at 12:12am UTC
http://www.cplusplus.com/doc/tutorial/files/ Look at the flags.
Topic archived. No new replies allowed.