How to Structure fstreams

Write your question here.
How would a fstream look if I needed to enter this data?

Flash_Card_One (Variable 52, Variable 26, Variable 54, Variable 24);
Flash_Card_Two (Variable 5, Variable 12, Variable 4, Variable 14);
Flash_Card_Three (Variable 5, Variable 30, Variable 41, Variable 13);
Flash_Card_Four (Variable 46, Variable 18, Variable 45, Variable 52);
Flash_Card_Five (Variable 41, Variable 55, Variable 19, Variable 26);

I'm not sure what your asking, can you try again ?
I think your asking how do I read a file that contains those lines and that would depend on what your trying to do.
Since I can not build a database I am having to use fstream instead. I need to build something??? that will help me have access to data while debugging my program. So I was hoping that someone could post what it would look like if I were to build a fstream with the above data.
Last edited on
I need to see how my "Flash_Card" date looks within a fstream. I'm not being difficult, I know next to nothing when it comes to programming. Despite that I am trying to solve a problem of my own making. It may come to me having to use something other than fstreams or C++ altogether. I'm truly in the dark.
Flash_Card_One (Variable 52, Variable 26, Variable 54, Variable 24);
What exactly is this representing? It looks a bit like C++ code, mainly because it ends with a semicolon. I don't see any context, is it meant to represent the construction of an object of type Flash_Card having the integer values 52, 26, 54, 24 perhaps?
"Flash_Card_One(Variable 52, 26, 54, 24); is a generalized representation. What I put into my program will not look like what you see. But it will have the same relevance. As said before I know very little C++. Just please present what I am asking for and let me figure out if it works in the long haul. At every stage of this process the program will evolve in complexity.
I guess I'm still unsure exactly what it is you are asking for.

Do you mean you have a text file which contains something like this, and that you'd like to read the data from that file into your program? Or is it something completely different to that?
52 26 54 24
5 12 4 14
5 30 41 13
46 18 45 52
41 55 19 26


I'm guessing here, just trying to make sense of what I've read so far.

I have a question too. You keep talking about fstream, what is it you think fstream does/is for ?
I think we have a misunderstanding there.
C++ fstream is for Input or output to files. fstream is not the command you use to read or write to files and maybe that is what your asking.

I need to see how my "Flash_Card" date looks within a fstream.

Please explain the above comment. To me a flash card is a "compact flash card" which holds files. The card wouldn't have a date, but the files would.
Hope this helps. I went to the store and bought a packet of 100 blank flash cards. On the first one I wrote a numerical 1 in the upper right corner and then a number 2 on the second blank flash card and so on until the 100th card. Then on each card I wrote 4 separate lines of information. Now I need my C++ program to be able to "store" this information. At first I thought that I would need to build a database to hold this information. Instead someone advised me to use a fstream. The mistake I made was presenting flash card 1 as if I were "defining" it, which if I were to define it, it would look like
Flash_Card_One (Variable 52, 26, 54, 24);
So my first question should be..... what must I do in order to make a C++ program hold data?
what must I do in order to make a C++ program hold data?

You need to define some variables in your program.
Then you need to have some way to put data into those variables.

Did you understand Chervil post? Chervil suggested using a text editor to create a text file containing your "flash cards". He even posted an example file containing 5 cards. Each row represents one card. In each row are 4 values representing the four values on a card.

Once you have created that text file with as many rows as you wish, you can use a stream to read the "cards" into your program.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

int main ()
{  int val[4];     //  Hold 4 values from one card
   ifstream ifs ("data.txt");  //  File to read values from
   
   while (ifs >> val[0] >> val[1] >> val[2] >> val3)    // read one card
   {    //  Do something with the flash card
   }
   return 0;
}

Last edited on
Let's pretend that I am compiling a group of animals. Each flash card may look like
Flash card 1
Dog
Ant
Pig
Shark

Would the "val[0]" still be valid in the most recent post?
In that case, you could store the data as a string. If I understand, you want something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <vector>

struct FlashCard {
    std::string val[4];    // hold 4 strings in one card
};

int main() {
    std::vector<FlashCard> cards; // a list of cards
    std::ifstream ifs("data.txt"); // file to get the cards from

    // read the card from the file
    FlashCard card;
    while (ifs >> card.val[0] >> card.val[1] >> card.val[2] >> card.val[3]) {
        cards.push_back(card); // add the card to the list
    }

    // Now you can look at the cards. Some useful things:
    std::cout << cards.size() << std::endl; // the number of cards you have
    std::cout << cards[0].val[0] << std::endl; // the first value in the first card

    return 0;
}


This would be reading from a file that looked something like this:
Dog Ant Pig Shark
Moose Duck Cow Chicken
Wombat Gazelle Crocodile Hamster

Where the first line is the first flash card (in cards[0]), the second line is the second flash card (in cards[1]), and the third line is the third flash card (in cards[2]).

So, the above program would print:
3
Dog


Does this help, or have I misunderstood your question?
Last edited on
Several questions.
1. If I used
using namespace std;
with
#include <iostream>
#include <string>
#include <vector>

couldn't I avoid having to type "std::"cout all the time an just type cout?


2. I think I need a void somewhere in TwilightSpectre example because I do not want my program to return any info such as

Dog Ant Pig Shark
Moose Duck Cow Chicken
Wombat Gazelle Crocodile Hamster
(ps, I don't know how you put up the greyed out boxes). am I right about the void?

I instead want the program to hold onto the data that I put into it. The functions that I plan on writing (with your help of course) later will refer to the data I have entered. Sorry for the lack of right type of info, but as you ask questions, only then can I know how precise I needed to be.

To recap what I need (for program only)
1. to store textual info and not have it be returned. that would be only the first phase (of many :( phases.
If someone would rather private msg me instead, I am open to that, thanks
Q1. Yes.

Q2. I don't think any of us really understand what you're trying to do. We've given you multiple examples of reading data from a file into arrays or vectors. You originally were talking about numbers, now you're talking about strings (animals).

You didn't respond when I asked if you understood Chervil's post. If you're not going to respond to questions asked of you, the chances of your getting further help decrease.

TwilightSpectre gave you an example of reading a text file of "flash cards" into a vector. I don't know what you mean by
I think I need a void somewhere in TwilightSpectre example because I do not want my program to return any info
. "return" doesn't make any sense. The only "return" in the program is at line 23 to signal the program is complete. If you don't want the program to display any information, just remove lines 20 and 21. If that's not what you mean, then you're going to have to do a better job of explaining what you mean.

After line 18, the "flash cards" exist in the vector, you can do anything you want with them.

You can create shaded boxes by using
[quote]text
[/quote] tags.
Last edited on
My access to internet is limited. So, if I do not respond it is not because I'm blowing it off. As said before

"Sorry for the lack of right type of info, but as you ask questions, only then can I know how precise I needed to be." and " I'm not being difficult, I know next to nothing when it comes to programming."

When I had displayed Flash Card as having "Variable 52 etc", I thought that it would be understood that variable 52 can take the place of virtually anything. except after seeing "val" being used in the response, which is when I brought up more precise info. which supports my statement

"Sorry for the lack of right type of info, but as you ask questions, only then can I know how precise I needed to be."

this would be so much easier if I could just discuss it with someone face to face so that I can show what I am trying to do. I simply just don't know enough about C++ to give a correct response. So if I explain it to someone maybe through private msg or here, that person may have a better understanding as to what needs to be done. I leave it up to you. my time is up see you tomorrow.
You've just restated what you have said before. You haven't clarified anything.

Programming is about specifying things clearly.

Try stating your problem in very simple terms, such as:
I want to read a series of flash cards from a text file.
Each flash card has four fields on it.
I want to do xxxxxx with each flash card.

Topic archived. No new replies allowed.