When I run this code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <fstream.h>
#include <iostream.h>
#include <time.h>
#include <string.h>
int main()
{
srand((unsigned)time(NULL));
int x=rand()%9;
ofstream value("xvalue.txt", ios::ate | ios::in, filebuf::sh_write );
if(!value.is_open())
{
cout<<"Could not open file... Error.... Quitting\n";
system("PAUSE");
return 0;
}
value<<x<<";";
value.close();
char yes;
cout<<"Would you like to view contents of file (up to 200 characters in)?\n y for yes, anything else for quit\n";
cin>>yes;
if (yes=='y' || yes== 'Y')
{
char text[200];
ifstream read_value("xvalue.txt", ios::out | ios::nocreate);
if (!read_value.is_open())
{
printf("Error opening file\n");
system("PAUSE");
return 0;
}
read_value>>text;
for (int i=0; i<=200; i++)
printf("%c", text[i]);
printf("\n\n");
read_value.close();
system("PAUSE");
printf("\n\nWould you like to delete the contents of the file?");
cin>>yes;
if (yes== 'y' || yes=='Y')
read_value.clear(0);
}
return 0;
}
I get what I want, but if I have any spaces in the file it stops reading there. Also, if I read a file it reads what's in it, and then adds some various characters. How can I fix both of these problems?