Archived

This topic is now archived and is closed to further replies.

diggisaur

Reading from a text file....

Recommended Posts

diggisaur    122
I try to read from a text file and get the following error. Warning: converting NULL to non-pointer type. I get this twice for each time the following line is compiled. fin.seekg(NULL, ios::beg); Here is the entire code for this tutorial. All help is appreciated greatly!!! ------------------------------------ #include <iostream> #include <string> #include <fstream> using namespace std; void main() { ifstream fin; string szLine = ""; string szWord = ""; string szName = ""; int health=0, gold = 0; fin.open("Stats.txt"); if(fin.fail()) { cout << "ERROR: Could not find Stats.txt!\n"; return 0; } cout << endl; while(fin >> szWord) cout << szWord; cout << endl; fin.clear(); fin.seekg(NULL, ios::beg); while(getline(fin, szLine)) cout << szLine << endl; cout << endl; fin.clear(); fin.seekg(NULL, ios::beg); fin >> szWord >> szName; fin >> szWord >> health; fin >> szWord >> gold; cout << "The Player''s name is: " << szName << endl; cout << "The Player''s health is: " << health << endl; cout << "The Player''s gold is: " << gold << endl; fin.close(); }

Share this post


Link to post
Share on other sites
Ronin_54    122
Okay, it took me some looking up, but I found the anwser

NULL is a compiler constant, that translates to the number 0. However, it''s only supposed to be used with pointers, to "park" them (make sure they point to nothing in case they are deleted or accessed when they shouldn''t be).

You are trying to assign NULL not to a pointer, but to a normal variabel (type: streamoff ). However, your compiler interprets NULL as belonging to a pointer, so it gives you a warning: ''Hey programmer, shouldn''t that variabel be a pointer?!''

Solution: remove NULL, and place a simple 0 (zero). ios::beg will then move you to the first location in the stream, and seekg will then move 0 bytes forward ^_^

(look at it this way: I don''t ask you to bring be NULL cookies, I would ask you to bring me 0 cookies (okay, bad example, I think I deserve at least 1 cookie now ))

Share this post


Link to post
Share on other sites
xjussix    122
quote:
Original post by Ronin_54
(look at it this way: I don''t ask you to bring be NULL cookies, I would ask you to bring me 0 cookies (okay, bad example, I think I deserve at least 1 cookie now ))



Haha! That was funny! :D Now I gotta clean up the monitor from all the coffee..

Share this post


Link to post
Share on other sites
diggisaur    122
Thanks guys that worked.

I thought I had tried swapping NULL from 0 already. Guess not. Must have been another NULL situation I was having problems with.

Share this post


Link to post
Share on other sites
Ronin_54    122
And thanks for telling us that it works ^_^ Many people don''t reply once you have given an anwser... though a quick ''it worked'' or ''it didn''t work'' can totally make our day ^_^

Thanks!

Share this post


Link to post
Share on other sites