-4

I am using struct but I do not know how to add information from a text files. Here is my basic struct. I am adding age and name to my struct.

 using namespace std;

struct stud1
{
    int age;
    char name[20];
};
int main()

{


    stud1 stud =
    {
        9,
        "Lee"

    };

cout<< stud.name <<endl;
}

At the end of the code Lee will be printed. I would like to add information but from a textfile. How do I add the first line of my text file to a struct?

Here is the text file:

10 John
12 Tim
11 Jack
SteveS112
  • 1
  • 1
  • 2
    [Google has 7,440,000 answers to this question.](https://www.google.com.au/search?q=read+text+from+file+c%2B%2B&rlz=1C1GGRV_enUS754US754&oq=read+text+from+file+c%2B%2B&aqs=chrome.0.0l6.3915j0j7&sourceid=chrome&ie=UTF-8) – Fantastic Mr Fox Jan 17 '18 at 03:39
  • 1
    Also it is asked on this website exactly 500 times a day. – Fantastic Mr Fox Jan 17 '18 at 03:40
  • Possible duplicate of [Read file line by line](https://stackoverflow.com/questions/7868936/read-file-line-by-line) – Fantastic Mr Fox Jan 17 '18 at 03:40
  • Clearly @FantasticMrFox 's 1st (and 2nd) comment is out of rage and not helpful. (perhaps, for getting a few likes from similar people. I've seen such responses a lot). I hope I'm wrong. It's misleading anyway. 7,440,000 is no true since if you press the next on Google there will not be that many results. This kind of answer is often expected from "SO famous people". I hope they work on their manner one day. However, you need to educate yourself about SO and how to use it properly. Also, google it first. Always. *I would not be surprised if this gets flagged. – Ali Abbasinasab Jan 17 '18 at 03:52
  • @AliAbbasinasab Well, to start with, the op has to show some relevant code. His question talks about a file. We didn't see any operations on files anywhere in the question. Expecting ready-made answers may lead to negative criticism in the form of downvotes. May be he should have a look at what makes an [\[ mcve \]](https://stackoverflow.com/help/mcve) in SO :-) – sjsam Jan 17 '18 at 04:01
  • 1
    @AliAbbasinasab, not out of rage, just frustration. Because when peoples first instinct is to ask on a forum, not to try to find a solution in anyway themselves, we end up with a big waste of time. The point of my comments was to convey that there is always supposed to be prior research put into the question, and this is how you should ask not only here, but any technical question. Try reading the [how to ask](https://stackoverflow.com/help/how-to-ask), the **very first** item is search and research. And clearly, none has clearly gone into this question. – Fantastic Mr Fox Jan 17 '18 at 04:31
  • @AliAbbasinasab In my opinion, telling the OP to Google for it first is useful (of course the How to ask page contains that, as pointed out above, but the OP don't bother to read it, so the question got 4 downvotes) – user202729 Jan 17 '18 at 04:40

1 Answers1

0

First of all according to the SO rules you should provide MCVE when asking a question. Secondly, you should avoid using namespace std; as it opens up the floodgates of global namespace pollution. You can do things like using std::cout; for convenience without opening up the whole standard namespace. Finally, life would be easier if you just used std::string instead of char[] in your struct but anyway here is the basic idea. You can read the age and name directly from your input file into local variables then set your structure elements from those input values. This is assuming that every line of your input file is as you say it is, and there is no error checking.

#include <fstream>  // for std::ifstream
#include <string>   // for std::string
#include <cstring>  // for strcpy()

std::ifstream infile("garbage.txt");
if (infile.is_open())
{
    int age;
    std::string namestr;
    while (infile >> age >> namestr)
    {
        stud1 temp;
        temp.age = age;
        strcpy( temp.name, namestr.c_str() );
        // do whatever before struct goes out of scope
    }
    infile.close();
}
Justin Randall
  • 2,243
  • 2
  • 16
  • 23