Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,537 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,527 people online right now. Registration is fast and FREE... Join Now!




Data From A File

 
Reply to this topicStart new topic

Data From A File, in_stream

cooplis
3 Dec, 2007 - 12:52 PM
Post #1

D.I.C Head
**

Joined: 11 Sep, 2007
Posts: 77


My Contributions
Two things, struct and in_stream.

This is what I have, I am attempting to get data from a file called checkin.dat and manipulate it.

CODE

#include <fstream>
#include <iostream>

double Getbegbal(ifstream& checkin.dat);

bal = Getbegbal("checkin.dat");

double Getbegbal(ifstream& checkin.dat)
{
in_stream.open("checkin.dat);
    return Begbal;
}


?????????????? blink.gif
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Data From A File
3 Dec, 2007 - 02:57 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Where is your main function? Where do you define bal? Where do you define Begbal? You are attempting to define your filename as a parameter to your function. You got to start with the basic framework and move on from there.

CODE

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

// Declaration for Getbegbal. Void return type because we are passing file by reference.
// Whatever we do to stream will be reflected back in main. No need to return anything.
void Getbegbal(ifstream& in_stream);

int main() {
     // Create a filestream
     ifstream filename;
    
     // Lets test if it is open (which it shouldn't be).
     if (filename.is_open()) {
          cout << "File is open" << endl;
          filename.close();
     }
     else {
          cout << "File is closed" << endl;
     }

     // Call our function to open the file.
     Getbegbal(filename);

     // Test again to see if the file is open. This time it is open, so we say it is open
     // and close it.

     if (filename.is_open()) {
          cout << "File is open" << endl;
          filename.close();
     }
     else {
          cout << "File is closed" << endl;
     }
}

// This function takes in a file stream and opens a file with it.
void Getbegbal(ifstream& in_stream)
{
    in_stream.open("checkin.dat");
    
}


This little example took your code and modified it to show you how to open a file stream using another function. First we create the stream variable, then test to see if the file is open. It isn't since we haven't opened it yet. We then call our function and pass it the closed stream. In the function we open it and since we passed by reference, filename back in main is opened. So again we test if the file is opened and this time it is, so we say it is open and close the file stream.

Step through it and look at my in code comments to see what is going on. It should make some sense after you read through it a couple times.

Enjoy!

"At DIC we be file opening and closing code ninjas!" decap.gif
User is offlineProfile CardPM
+Quote Post

cooplis
RE: Data From A File
4 Dec, 2007 - 05:21 AM
Post #3

D.I.C Head
**

Joined: 11 Sep, 2007
Posts: 77


My Contributions
QUOTE(Martyr2 @ 3 Dec, 2007 - 03:57 PM) *

Where is your main function? Where do you define bal? Where do you define Begbal? You are attempting to define your filename as a parameter to your function. You got to start with the basic framework and move on from there.

CODE

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

// Declaration for Getbegbal. Void return type because we are passing file by reference.
// Whatever we do to stream will be reflected back in main. No need to return anything.
void Getbegbal(ifstream& in_stream);

int main() {
     // Create a filestream
     ifstream filename;
    
     // Lets test if it is open (which it shouldn't be).
     if (filename.is_open()) {
          cout << "File is open" << endl;
          filename.close();
     }
     else {
          cout << "File is closed" << endl;
     }

     // Call our function to open the file.
     Getbegbal(filename);

     // Test again to see if the file is open. This time it is open, so we say it is open
     // and close it.

     if (filename.is_open()) {
          cout << "File is open" << endl;
          filename.close();
     }
     else {
          cout << "File is closed" << endl;
     }
}

// This function takes in a file stream and opens a file with it.
void Getbegbal(ifstream& in_stream)
{
    in_stream.open("checkin.dat");
    
}


This little example took your code and modified it to show you how to open a file stream using another function. First we create the stream variable, then test to see if the file is open. It isn't since we haven't opened it yet. We then call our function and pass it the closed stream. In the function we open it and since we passed by reference, filename back in main is opened. So again we test if the file is opened and this time it is, so we say it is open and close the file stream.

Step through it and look at my in code comments to see what is going on. It should make some sense after you read through it a couple times.

Enjoy!

"At DIC we be file opening and closing code ninjas!" decap.gif

Thank you.

I am going to do some testing with my file and then I need a little help with my struct. Also, do you have to do return in a while loop, if you are pulling from a file?
User is offlineProfile CardPM
+Quote Post

cooplis
RE: Data From A File
4 Dec, 2007 - 06:36 AM
Post #4

D.I.C Head
**

Joined: 11 Sep, 2007
Posts: 77


My Contributions
QUOTE(cooplis @ 4 Dec, 2007 - 06:21 AM) *

QUOTE(Martyr2 @ 3 Dec, 2007 - 03:57 PM) *

Where is your main function? Where do you define bal? Where do you define Begbal? You are attempting to define your filename as a parameter to your function. You got to start with the basic framework and move on from there.

CODE

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

// Declaration for Getbegbal. Void return type because we are passing file by reference.
// Whatever we do to stream will be reflected back in main. No need to return anything.
void Getbegbal(ifstream& in_stream);

int main() {
     // Create a filestream
     ifstream filename;
    
     // Lets test if it is open (which it shouldn't be).
     if (filename.is_open()) {
          cout << "File is open" << endl;
          filename.close();
     }
     else {
          cout << "File is closed" << endl;
     }

     // Call our function to open the file.
     Getbegbal(filename);

     // Test again to see if the file is open. This time it is open, so we say it is open
     // and close it.

     if (filename.is_open()) {
          cout << "File is open" << endl;
          filename.close();
     }
     else {
          cout << "File is closed" << endl;
     }
}

// This function takes in a file stream and opens a file with it.
void Getbegbal(ifstream& in_stream)
{
    in_stream.open("checkin.dat");
    
}


This little example took your code and modified it to show you how to open a file stream using another function. First we create the stream variable, then test to see if the file is open. It isn't since we haven't opened it yet. We then call our function and pass it the closed stream. In the function we open it and since we passed by reference, filename back in main is opened. So again we test if the file is opened and this time it is, so we say it is open and close the file stream.

Step through it and look at my in code comments to see what is going on. It should make some sense after you read through it a couple times.

Enjoy!

"At DIC we be file opening and closing code ninjas!" decap.gif

Thank you.

I am going to do some testing with my file and then I need a little help with my struct. Also, do you have to do return in a while loop, if you are pulling from a file?


Ok it gives me the file is closed twice. where you list filename should I be changing that to check.dat. I have to get info from the file I would like to see it on the screen should I use fout? Also it does not like "void Getbegbal(ifstream& in_stream);" function.

CODE


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

void Getbegbal(ifstream& in_stream);

ifstream in_stream;
ofstream out_stream;

int main()
{

out_stream.setf(ios::fixed);
out_stream.setf(ios::showpoint);
out_stream.precision(2);

  
     ifstream filename;
    
          if (filename.is_open()) {
          cout << "File is open" << endl;
          filename.close();
     }
     else {
          cout << "File is closed" << endl;
     }

  
     Getbegbal(filename);
    
     if (filename.is_open()) {
          cout << "File is open" << endl;
          filename.close();
     }
     else {
          cout << "File is closed" << endl;
     }
     system("pause");
}


void Getbegbal(ifstream& in_stream)
{
    in_stream.open("checkin.dat");
    
}


Thanks
User is offlineProfile CardPM
+Quote Post

cooplis
RE: Data From A File
4 Dec, 2007 - 11:05 AM
Post #5

D.I.C Head
**

Joined: 11 Sep, 2007
Posts: 77


My Contributions
void Getbegbal(ifstream& in_stream);

what's wrong with this statement?
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 09:14PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month