[Solved] File I/O Problem

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

[Solved] File I/O Problem

Post by zodiac976 »

I am having a tough time figuring out this little problem.

Test * ptrTest = new Test;

ifstream file("test.txt");
file >> ?.... Here is the problem

ptrTest->setNum(); The parameter in this takes the specified
data type but is there anyway I can do something like this:

ifstream file("test.txt");
file >> ptrTest->setNum();
or
ptrTest->setNum(file);

I just need to input the data from the text file one line at
a time into each variable indirectly through accessors.

I might can do this by overloading the extraction operator ">>"
but I can't find anything on google to explain how to overload
it if that will even help me accomplish what I want to do :|.
Last edited by zodiac976 on Tue Jun 30, 2009 3:36 pm, edited 1 time in total.
Scoody
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 65
Joined: Fri Feb 06, 2009 2:07 pm

Re: File I/O Problem

Post by Scoody »

If you got a C++ book, overloading should be covered there. The alternative is to store the data in an intermediate variable and pass that variable to the setNum function or pass "file" as a reference/pointer and do your operation on it directly within the setNum function.
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: File I/O Problem

Post by zodiac976 »

Yea my book mentions overloading operators but it never
shows how to overload the extraction symbols ">>". I did
make extra variables to temporarily store it but that sure
does take up a lot of space and time, just wondering if
there is a more compact solution. I will look into making
file a pointer and see what it can do.
User avatar
thejahooli
Chaos Rift Junior
Chaos Rift Junior
Posts: 265
Joined: Fri Feb 20, 2009 7:45 pm
Location: London, England

Re: File I/O Problem

Post by thejahooli »

It should only take two extra lines if I understand what you're doing

Code: Select all

ifstream file("test.txt");
int num;
file >> num;
ptrTest->SetNum(num);
pass "file" as a reference/pointer and do your operation on it directly within the setNum function.
You could do this but then you would have the extra hassle of having to pass the file and also you would have to overload the function, which I try to avoid if possible as it makes it harder for me to remember what the function parameters are, but to do it this way it would also be quite simple.

Code: Select all

Test::SetNum(ifstream &file)
{
	file >> this->num;
}
I'll make your software hardware.
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: File I/O Problem

Post by zodiac976 »

Yea but if I have like 10 variables that would be around
20 lines :/. Thank you I will try the latter..
Last edited by zodiac976 on Tue Jun 30, 2009 3:36 pm, edited 1 time in total.
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: File I/O Problem

Post by zodiac976 »

thejahooli wrote:It should only take two extra lines if I understand what you're doing

Code: Select all

ifstream file("test.txt");
int num;
file >> num;
ptrTest->SetNum(num);
pass "file" as a reference/pointer and do your operation on it directly within the setNum function.
You could do this but then you would have the extra hassle of having to pass the file and also you would have to overload the function, which I try to avoid if possible as it makes it harder for me to remember what the function parameters are, but to do it this way it would also be quite simple.

Code: Select all

Test::SetNum(ifstream &file)
{
	file >> this->num;
}
Hey thanks I went and plugged in this and it worked like a charm
here's an example:

Code: Select all

inline void setNum(ifstream &file) {file >> this->num;}

ifstream file("test.txt");
     ptrObject->setNum(file);
file.close();
Worked perfectly thank you a lot :bow:.
User avatar
thejahooli
Chaos Rift Junior
Chaos Rift Junior
Posts: 265
Joined: Fri Feb 20, 2009 7:45 pm
Location: London, England

Re: File I/O Problem

Post by thejahooli »

zodiac976 wrote:Yea but if I have like 10 variables that would be around
20 lines :/. Thank you I will try the latter..
The method you used worked fine so I would advise that you use it, but for future reference you could use a single variable to read different values from the file.

e.g.

Code: Select all

int temp;

file >> temp;
ptrTest->SetNum1(temp);
file >> temp;
ptrTest->SetNum2(temp);
This way you could use a single variable to read different numbers from the file and then assign that to different variables.
I'll make your software hardware.
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: File I/O Problem

Post by zodiac976 »

thejahooli wrote:
zodiac976 wrote:Yea but if I have like 10 variables that would be around
20 lines :/. Thank you I will try the latter..
The method you used worked fine so I would advise that you use it, but for future reference you could use a single variable to read different values from the file.

e.g.

Code: Select all

int temp;

file >> temp;
ptrTest->SetNum1(temp);
file >> temp;
ptrTest->SetNum2(temp);
This way you could use a single variable to read different numbers from the file and then assign that to different variables.
That's why I created seperate variables inside my class
to cover that but I see what you mean..I guess it can
work either way.
Scoody
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 65
Joined: Fri Feb 06, 2009 2:07 pm

Re: File I/O Problem

Post by Scoody »

zodiac976 wrote:Yea my book mentions overloading operators but it never
shows how to overload the extraction symbols ">>".
I have "Starting out with C++ - Early objects" from my C++ class, and it shows how to overload each operator. Though I'm not 100% sure, the "Standard C++ bible" I have does it too. Maybe you need a book upgrade? :)

Anyway..

Code: Select all

#include <iostream>
#include <fstream>
using namespace std;

class Test {
  public:
    int getLol() { return lol; };
    friend istream &operator>>(istream& strm, Test& test);
  private:
    int lol;
};

int main() 
{
  Test rofl;
  ifstream lol("lol.txt");

  lol >> rofl;
  cout << rofl.getLol() << endl;

  return 0;
}

istream &operator>>(istream& strm, Test& test)
{
  strm >> test.lol;
  return strm;
}
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: [Solved] File I/O Problem

Post by dandymcgee »

doublecyrax wrote:too easy scoody, too fucking easy...

fuck. i wont even bother with this easy ass garbage. call me when you learn how to program, stupid.
What the hell? :lol:
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: File I/O Problem

Post by zodiac976 »

Scoody wrote:
zodiac976 wrote:Yea my book mentions overloading operators but it never
shows how to overload the extraction symbols ">>".
I have "Starting out with C++ - Early objects" from my C++ class, and it shows how to overload each operator. Though I'm not 100% sure, the "Standard C++ bible" I have does it too. Maybe you need a book upgrade? :)

Anyway..

Code: Select all

#include <iostream>
#include <fstream>
using namespace std;

class Test {
  public:
    int getLol() { return lol; };
    friend istream &operator>>(istream& strm, Test& test);
  private:
    int lol;
};

int main() 
{
  Test rofl;
  ifstream lol("lol.txt");

  lol >> rofl;
  cout << rofl.getLol() << endl;

  return 0;
}

istream &operator>>(istream& strm, Test& test)
{
  strm >> test.lol;
  return strm;
}
Thanks I will have to look this code over to understand it.
Just a couple of lines that is..I never had to overload anything
except functions or constructors which is easy but nothing like
operator overloading...
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: [Solved] File I/O Problem

Post by zodiac976 »

doublecyrax wrote:too easy scoody, too fucking easy...

fuck. i wont even bother with this easy ass garbage. call me when you learn how to program, stupid.
If you can't say something informative and nice don't say
anything at all. You will just make more enemies that way.
Scoody
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 65
Joined: Fri Feb 06, 2009 2:07 pm

Re: [Solved] File I/O Problem

Post by Scoody »

It's not that hard, but you should get a book which discusses the topic, makes it much easier.
When you use friend-functions they have access to all the variables in the class like a class-function, so this way you're allowed to change values within the operator-function.
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: [Solved] File I/O Problem

Post by zodiac976 »

Scoody wrote:It's not that hard, but you should get a book which discusses the topic, makes it much easier.
When you use friend-functions they have access to all the variables in the class like a class-function, so this way you're allowed to change values within the operator-function.
I am definitely overdue for a good book :lol:.
I understand friends just not quite the other part...
Post Reply