Help loading from text file!

This is a discussion on Help loading from text file! within the C++ Programming forums, part of the General Programming Boards category; Im having trouble when trying to load a database froma text file. i dont know how to do this. i ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    44

    Help loading from text file!

    Im having trouble when trying to load a database froma text file. i dont know how to do this. i tryed everythings, even with seekg and tellg, but i cannot find a way. this is the text file:

    Code:
    ======================================================
    First Name = Tomas
    Last Name = Cordeviola
    Salary = 5000
    Meeting per week = 3
    Vacation days per year = 30
    ======================================================
    First Name = Agustin
    Last Name = Perez
    Salary = 3000
    Meeting per week = 2
    Vacation days per year = 27
    ======================================================
    First Name = Fernando
    Last Name = Blanco
    Salary = 2000
    Knows C++ = No
    Years of Experience = 3
    Type of Engineer = Electric
    ======================================================
    First Name = Joaquin
    Last Name = Estevan
    Salary = 1800
    Knows C++ = No
    Years of Experience = 2
    Type of Engineer = Mechanical
    ======================================================
    First Name = Stefania
    Last Name = Dominguez
    Salary = 2100
    PhD recieved in = UP
    PhD thesis topic = Clothes
    ======================================================
    First Name = Fiorella
    Last Name = Pertusati
    Salary = 1900
    PhD recieved in = UBA
    PhD thesis topic = Something
    that file is saved from the same script id like to load it. the problem is that there are three different classes, and each employee is part of one of those, but how can i know which class is he one i need to create to load that employee? The classes i have are: Manager, Engineer and Researcher. the first 2 are managers, the 3rd and 4th are Engineers and the 5th and 6th are Researchers. I tryed to identify which was the class needed by searching for the words "Meeting", "Knows" or "PhD" but got problems with that.
    How can i do this? please help! if you want i can post the full code..it isnt long.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I think you get a line and then split it into the key and value. Then you look at what kind of keys you got for each entry,
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    44
    oh ya its a good idea...something like this for example?:
    Code:
    string s;
    getline(inFile, s);
    int length = s.length();
    string key = s.substr(0,13);
    string value = s.substr(13, length-13);
    and how can i know which is the class of the object i have to create(Manager, Engineer or Researcher)?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Read in all the keys (not just one!) and then look to see which of your "special" keys was included. (Your belief that the key is the first thirteen characters, rather than up to the first = sign, is confusing to me.)

    Alternately, yell at the people writing the file out to include a key like Type = Manager.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    44
    tabstop i know i have to read in all the keys, that was just an example, and how can i do a code to read until the '='? something like this? this is just for the second line(First name):
    Code:
    string key;
    getline(inFile,key,'=');
    string value;
    getline(inFile,value);
    Will the value have at the first char a blank space? if so, how can i prevent that?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The key will end with a blank space, and the value will start with a blank space. You can use erase to get rid of characters inside a string. It wouldn't be a bad idea, just in case, to check first/last letter before erasing it to make sure it's a space.

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    44
    ok thatnk you very much, and by the way, what does fflush() does? cause i had a problem with getline but i solved using it like this:
    Code:
    	fflush(stdin);
    	cout << "School: ";
    	getline(cin, mSchool);
    	cout << "Thesis: ";
    	getline(cin,mThesis);
    if i dont add fflush it would skip the first getline...is this a wrong way of solving it? or is it fine? i dont know, cause i dont know what fflush does...

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    If you want to 'flush' the input stream, then use cin.ignore(), not fflush(stdin)
    http://www.cplusplus.com/reference/i...am/ignore.html

    > if i dont add fflush it would skip the first getline
    That's because you're mixing input methods
    For example
    cin >> myInt; // leaves a trailing \n for getline() to eat immediately

    Which, if you follow with a getline call results in the first getline being 'skipped'

    > is this a wrong way of solving it?
    Using .ignore() would be far better.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    44
    Thank you salem. Ill use .ignore() now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Loading text from file into struct
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 03-19-2002, 04:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21