Jump to content

How to store text file information in C++ data structure?

- - - - -

  • Please log in to reply
7 replies to this topic

#1
bexita

bexita

    Newbie

  • Members
  • PipPip
  • 15 posts
Hi,

For exp, I got this text file containing & wanted to store different variables in a row under my struct:

John Son [tab] Mr [tab] CEO [tab] 98493834
Francis Ng [tab] Mr [tab] Coordinator [tab] 98493834
John Andrew [tab] Mr [tab] Application processing [tab] 98493834

My structure :
struct Staff

{

    char name;

    char title;

   char position;

};


// file.open text file & write it binary mode

Staff a;

//I want to store different variables in a row but still cannot get

while (file >>s.name)

{ file>> s.title>>s.position

}

After that I output into another text file but the content is messy. Whats wrong with my input ?
any advise? thx:crying:

Edited by Alyn, 19 October 2011 - 01:58 AM.
added code tag


#2
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US

bexita said:

struct Staff

 {

 char name;

 char title;

 char position;

 };

I'm not sure if this is the problem, but aren't you supposed to use 'char name [512];' , or something, rather than 'char name;' ? Because what you currently have seems like name and title and position are one-character values.



By the way, please use CODE tags, when posting code. (For example, instead of typing:
print "Hello World!\r\n";

you type:
[NOPARSE]
print "Hello World!\r\n"; 
[/NOPARSE]

and this forum would automatically parse that, so it would turn into this:
print "Hello World!\r\n"; 
.)

#3
HemStar

HemStar

    Newbie

  • Members
  • Pip
  • 3 posts
We cannot use a Single char variable to take too many characters.Each time you take data from file and give it as a value to char variable it replaces the old character which previously it war storing with the new character from the file.So you cannot get all the data printed out when you print that char variable it only prints the last stored value.I hope this may help you

#4
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
Instead of 'char name', 'char title', and 'char position' (which would just be single characters) you may want to use 'string name', 'string title', and 'string position. You would also have to include the string header.


#include <string>

using namespace std;


struct Staff

{

    string name;

    string title;

    string position;

};


Latinamne loqueris?

#5
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Since you're coding in C++, why not use std::string? Also, I don't think >> and << operators will work properly on binary files.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#6
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
What's the C++ notion of fread () and fwrite () ?

#7
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
read() and write() methods of std::stream class.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#8
Muted

Muted

    Learning Programmer

  • Members
  • PipPipPip
  • 86 posts

bexita said:

After that I output into another text file but the content is messy. Whats wrong with my input ?
any advise? thx:crying:

Hello, bexita.

Creating file input/output with C++'s file streams (even for binary input/output) is VERY simple and relatively easy!
Here, I've taken the liberty of rewriting your program for you (with slight modifications along the way):
#include <iostream>
#include <fstream>

// You could use std::string instead of char*/char[] here as well
struct STAFF {
    char* name;
    char* title;
    char* position;

    STAFF() { }
    STAFF(char* name, char* title, char* position) : name(name), title(title), position(position) { }
};

int main() {

    // Our single, lonely employee :(
    STAFF staff("bexita", "Programmer", "Newbie");

    // A binary stream to write output to
    std::fstream file;

    // Open the file up for binary output (checking for an error state)
    file.open("staff.bin", std::ios::out | std::ios::app | std::ios::binary);
    if (!file.good()) {
        file.close();
        std::cerr << "Error opening file! Aborting program...\n";
        return 0;
    }

    // Write our single STAFF structure to the file (checking for an error state)
    file.write((char*)&staff, sizeof(STAFF));
    if (file.bad()) {
        file.close();
        std::cerr << "Error writing to file! Aborting program...\n";
        return 0;
    }

    // Close the file, and exit the program! :)
    file.close();

    return 0;
}

If you're honestly wanting to learn more about binary file input/output with C++, here are some resources to get started with:
- ostream - C++ Reference (Reference: "Unformatted output")
- C++ Binary File I/O (Bottom of the page, should jump on its own)

Google's a great resource. Have fun, and good luck (and belated merry Christmas)!! :)
“You may be disappointed if you fail, but you are doomed if you don't try.”
- Beverly Sills




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users