-
File Reading Errors
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?
-
First of all...
You are mixing C and C++ I/O. This is not good practice. The function printf from the stdio library is a C function. Use cout instead. The declaration of read_value(file, ios::in) to open a file is also a C type function. Use read_value.open(file, ios::in) instead.
Secondly, your app quits reading at whitespaces because the >> operator treats them as a delimiter. To avoid this use either the file.getline() or file.get() function.
Here is the reason you're getting garbage for output:
Code:
for (int i=0; i<=200; i++)
printf("%c", text[i]);
You're telling printf to output 199 chars when you have only 2 (or however many are stored in your file) stored in the array. The rest of the chars you're outputting are null characters.
Lastly, fstream.clear() doesn't delete the contents of a file like you might think it would. The clear function is actually used to clear the filestream buffer.
Take a close look at the code below. It's well commented.
Code:
//include our headers using the standard namespace
#include <cstdlib>
#include <fstream>
#include <iostream>//exclude <stdio.h> since we're using <iostream>
#include <ctime>
using namespace std;
//the std namespace also includes some basic
//string functions like stricmp() and stcmp()
int main()
{
ofstream value;//declare our output stream
ifstream read_value;//declare our input stream
char yes[1];//set aside an array for yes
int x;//for our random number
srand((unsigned)time(NULL));
x = rand()%9;
value.open("xvalue.txt", ios::out | ios::app );
//use the open function, app is preferred to ate
if(!value.is_open())
{
cout << "Could not open file... Error.... Quitting\n";
exit(0);//exit if our file doesn't open
}
value << x << ";";
value.close();
cout << "Would you like to view contents of file "
<< "(up to 200 characters in)?\n\nY for yes, "
<< " anything else for quit: ";
cin >> yes;
cin.get();
//cin leaves the \n character in the buffer,
//so we call cin.get to remove it
cout << "\n";
if (stricmp(yes, "Y") == NULL)
//"stricmp()" compares two character strings.
//Case is ignored as opposed to "strcmp(),"
//which is case sensitive. It returns
//NULL or 0 if the two strings are equal.
{
char text[200];
read_value.open("xvalue.txt", ios::in );
if (!read_value.is_open())
{
cout << "Error opening file\n";
exit(0);
}
read_value.getline(text, 200);
//here we pass our character array and its size to
//read_value.getline(). This will read characters into
//the array until it reaches the delimiter. The delimiter
//is set to "\n" by default but can be changed by
//overloading the function.
cout << text << "\n\n";
//output the text to the screen and we are done.
//Good work!
read_value.close();
}
return 0;
}
-
OK, that works...
But there is still one problem...
A runtime error....
After it displays the file, I get:
Run-Time Check Failure #2 - Stack around the variable 'yes' was corrupted.
And then it freezes for a while and exits.
[EDIT]
I'm am using VC++ .NET. It's being run in debug mode. When I do it in release it works just fine...
[/EDIT]
-
Hmm...
It sounds like you're exceeding the bounds of your array. Tell me if I'm wrong, but and array of size[1] has two members [0] and [1] right? So the "Y" the user enters gets stored in [0] and [1] is reserved for the null char.
Did you change the size of the "yes" array?
Perhaps try increasing the variable size of "yes" to yes[2]. Or bigger, just in case the user tries to enter garbage. Say yes[20].
Let me know if that doesn't work.
-
It worked, but before I was just just typing y and I got that error. Oh well, it works now. Also how would I make a sub-directory?
ex:
the .exe is at C:\MyProgs\Prog00\
and I want the text file at C:\MyProgs\Prog00\Saves
How would I do that?
-
It's simple :)
value.open("saves/xvalue.txt", ios::in | ios::app);
That's all there is to it.
-
Oh I was trying to use the backslash:
value.open("saves\\xvalue.txt", ect)
the two back-slashes is obvious, the escape character.
Just wondering: Was the Dev C++ compiler created using assembly or C++? I'm asuming assembly.
-
I tried your directory thing but it didn't work. I don't know why.
-
To my knowledge, but don't quote me, compilers are created in assembly yes. The only langauges that are based on other langauges are called scripts. Again don't quote me.
Are you using a double forward slash like you did in the your example above? Or were you just trying to escape to back-slash in that example? It is one forward-slash between the directory and the file surrounded in quotes. At least on my compiler and system. Using VS6 on XP.
Either way, did you create a folder called "saves" and put your file in it?
-
OK, it's working now, I didn't know you had to create the folder. I thought it would make it for you like it makes files for you if they don't exsist
-
Another question: How do you run .exe from a .exe with C++.
http://cprogramming.com/boardfaq.html#execs
It tells you how to run exes from a exe.
I've tried both but neither work. Is there a way that works?
BTW: Something else useful there is it tells you how to make a gotoxy() function, it's quite simple really
-
The syntax for using the system command is different from opening a file, in this case you do use the escape character. You must also specify the directory where the exec is located. Unless it's in the same folder as the one you're running I think.
system("C:\\Games\\StarWarsJK2\\JediOutcast.exe");