#include <windows.h>
#include <fstream>
#include <iostream>
using namespace std;
#include <stdio.h>
int main()
{
char temp[1];
ofstream outfile;
outfile.open("text.txt", ios::out);
outfile<<20;
outfile.close();
ifstream infile;
infile.open("test.txt", ios::in);
infile>>temp;
cout<<temp<<endl;
cout<<(int)temp<<endl;
cout<<atoi(temp);
return 0;
Basic File Loading (again)
Okay, I feel really dumb. I''m trying to write a program that can open in and read in text from a text file (different from the .bmp loader I posted it about earlier. Here is the code I am using to better illustrate my goal:
I can write to a file fine; I checked to make sure the file was in the right place and had the write contents, and it did. In this example, the text file to be opened merely has the content "20".
Then, I open the text file to read its contents. I first read in as a char. I output that and it reads "H"... hmm, that doesn''t make sense. I would''ve expected it to display whatever char an ASCII value of ''2'' is (the first value of the file)
Then, just out of curisoity, I cast the char as an int, just out of curiosity. It displays ''72'' (I would have expected a ''2'', since it should have read that first) I tried a similar setup with the fopen type functions -- same result.
Any ideas? I feel really stupid for asking this, as I''d like to think of myself as moving past the newbie stage -- apparently not I''ve done this before too, and I could''ve sworn you do it like this. Does the fact that I''m on Windows XP now make any difference? I''m totally out of ideas
Thanks in advance
Eric Farraro
Peon
Peon
welcome,im stuc with a ifstream to.
int length;
char * temp
temp = new char [length]; //allocate memory
try infile.read(temp,length)
or infile.get(xxx)
with a little help from:
http://www.cplusplus.com/ref/iostream/istream/
int length;
char * temp
temp = new char [length]; //allocate memory
try infile.read(temp,length)
or infile.get(xxx)
with a little help from:
http://www.cplusplus.com/ref/iostream/istream/
I can''t see where the 72/''h'' is coming from, but this line looks suspect:
That''s an "array of 1 char" (instead of "a char"). Effectively it''s a 1 character string, which only has room for a null terminator.
But then I got to this bit...
Take a very close look at those filenames.
char temp[1];
That''s an "array of 1 char" (instead of "a char"). Effectively it''s a 1 character string, which only has room for a null terminator.
But then I got to this bit...
outfile.open("text.txt", ios::out); outfile<<20; outfile.close(); ifstream infile; infile.open("test.txt", ios::in);
Take a very close look at those filenames.
You really should make it test for errors:
----------------
-Earth is 98% full. Please delete anybody you can.
My Site
int main(){ ofstream outfile; ifstream infile; outfile.open("test.txt", ios::out); if (!outfile.is_open()) { cout << "Cannot open output file" << endl; return (0); } outfile << 20; if (outfile.fail()) { cout << "Failed to write data to file" << endl; } outfile.close(); infile.open("test.txt", ios::in); if (!infile.is_open()) { cout << "Cannot open input file" << endl; return (0); } infile >> temp; if (outfile.fail()) { cout << "Failed to read data from file" << endl; } }
----------------
-Earth is 98% full. Please delete anybody you can.
My Site
Krunk: Yea, I''m officially an idiot... lol. That actually wasn''t the problem with the actual code (this was just an example) but I got it to work as well... dunno how I did, or what I did wrong before, but thanks for your help
Sand Hawk: I really need to get into error checking huh? I think it''s because I''m still a newbie and used to writing text based stuff where errors were easier to track down I guess since I''d like to move on to things that are a little more difficult, I should start doing this. Good advice.
Sand Hawk: I really need to get into error checking huh? I think it''s because I''m still a newbie and used to writing text based stuff where errors were easier to track down I guess since I''d like to move on to things that are a little more difficult, I should start doing this. Good advice.
Peon
quote: Original post by Krunk
I can''t see where the 72/''h'' is coming from, but this line looks suspect:char temp[1];
That''s an "array of 1 char" (instead of "a char"). Effectively it''s a 1 character string, which only has room for a null terminator.
But then I got to this bit...outfile.open("text.txt", ios::out); outfile<<20; outfile.close(); ifstream infile; infile.open("test.txt", ios::in);
Take a very close look at those filenames.
temp[0] =
temp[1] = \0
so you lied.
.lick
char whatever[10] goes from whatever[0] to whatever[9].
char whatever[1] goes from whatever[0] to whatever[0], as I have understood it.
char whatever[1] goes from whatever[0] to whatever[0], as I have understood it.
Super Sportmatchen - A retro multiplayer competitive sports game project!
That''s confused me to no ends... When declaring an array, is the "intializer"? the number of elements, or the nubmer it goes up to? I''m guessing it''s the number of elements (blah [0] up there is probably a dumb mistake by me ) So yea, I''m guessing Bauer is right but I might be wrong.
Peon
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement