Sign in to follow this  

fstream

This topic is 4717 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

All I have to say is wow, I wrote a simple read in file and it gave me the wierdest output i've ever seen. Take a look.
#include <fstream>
#include <iostream>
using namespace std;
int main() {
ifstream infile;
infile.open("myinput.txt");
int map[10][10];
for(int x=0; x<10; x++) {
  for(int y=0; y<10; y++) {
    infile>>map[x][y];
    cout<<map[x][y];
  }
}
}

output is
C:\Documents and Settings\HP_Owner\Desktop\C++>fstream
01111111101222222101233332101234432101234432101233332101222222101111111100-12089
87934620898792112089879275260200947323222936722089913856-12089913850208986678920
898720792293040165559000000-204106705600-204808904800-1-1478238102360896-1-14769
2344-2099912-2141960028-2046676734-21422654260-2046676620-204362419200-204667662
040070000040070164006984-2043624120-14377361760835358154007008399895214400702439
98968131439989521643998968399901623718322141114146442293304020898789844006984229
3508208987886539994642089878893040069922368416229367239989683999016
C:\Documents and Settings\HP_Owner\Desktop\C++>

crazy lookin eh?

Share this post


Link to post
Share on other sites
hi,
well if your file contains numbers in text format then the output might be perfectly what i would expect.
if you store values this way you need to read them in a string first and then convert them to int.

Share this post


Link to post
Share on other sites
would you mind to paste in your data-file? this would make things a bit easier...

I don't know if your char approach would work because char is just a numerical type as int is.

do it like:

string data;

file >> data; //reads a complete line from the file



then use string functions to read the various elements of your grid from the string

Share this post


Link to post
Share on other sites
fixed,

#include <fstream>
#include <iostream>
using namespace std;
int main() {
ifstream infile;
infile.open("myinput.txt");
char map[10][10];
for(int x=0; x<10; x++) {
for(int y=0; y<10; y++) {
infile>>map[x][y];
cout<<map[x][y];
if(y == 9)
cout<<"\n";
}
}
}



edit: How can I make it newline inside the file?

Share this post


Link to post
Share on other sites
hm...
I haven't worked with text files much but my guess is that non-text characters are skipped if you read the file like this. :/

even if you read a whole line into a string the string doesn't contain the "\n" character afterwards...

Share this post


Link to post
Share on other sites
hm, well since I got it to work, I decided to write a simple tile based engine real quick, but it wont work, I dont know what else i need to do.

#include <iostream>
#include <fstream>
#include <SDL/SDL.h>
using namespace std;
SDL_Surface *screen;
SDL_Surface *grass;
SDL_Surface *water;
SDL_Surface *tree;
SDL_Surface *water2;
int xpos, ypos;
int initImages() {
grass = SDL_LoadBMP("grass.bmp");
water = SDL_LoadBMP("water.bmp");
tree = SDL_LoadBMP("tree.bmp");
water2 = SDL_LoadBMP("water2.bmp");
return 0;
}
int SDL_BlitSurface(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dest, SDL_Rect *destrect);
void drawIMG(SDL_Surface *img, int x, int y) {
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_BlitSurface(img, NULL, screen, &dest);
}
void opentxt() {
ifstream map;
map.open("myinput.txt");
char mymap[10][10];
for(int x=0; x<10; x++) {
for(int y=0; y<10; y++) {
map>>mymap[x][y];
if(mymap[x][y] == 0) {
drawIMG(water2, xpos, ypos);
SDL_Flip(screen);
}
if(mymap[x][y] == 1) {
drawIMG(water, xpos, ypos);
SDL_Flip(screen);
}
if(mymap[x][y] == 2) {
drawIMG(grass, xpos, ypos);
SDL_Flip(screen);
}
if(mymap[x][y] == 3) {
drawIMG(tree, xpos, ypos);
SDL_Flip(screen);
}
}
}
}
int main(int argc, char *argv[]) {
Uint8* keys;
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
cout<<"Cannot initialize SDL VIDEO."<<endl;
SDL_GetError();
}
atexit(SDL_Quit);
screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
if(screen == NULL) {
cout<<"Unable to set video mode."<<endl;
SDL_GetError();
}
initImages();
int done = 0;
while(done == 0) {
SDL_Event event;
while(SDL_PollEvent(&event)) {
if(event.type == SDL_QUIT) { done = 1; }
if(event.type == SDL_KEYDOWN) {
if(event.key.keysym.sym == SDLK_ESCAPE) { done = 1; }
}
}
opentxt();
}
return 0;
}




EDIT: Yes I know its very messy code.

Share this post


Link to post
Share on other sites

This topic is 4717 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this