Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 09-27-2006, 12:44 PM   #1 (permalink)
dave120
Recruit
 
Join Date: Sep 2006
Posts: 10
dave120 is on a distinguished road
Need help with reading from file

Hello,

I'm writing a database program using hashing, and I need to make it read in a file with a whole lot of names in it. Each field is separated by a space, and I'm not sure how to make the program read it in this way. The only way I've done file input before was with the getline function where each thing is on a different line.

The way I've used it before is like this:

for(int j=0; j<nItemsFile; j++)
{
infile.getline(tmpFirst,31);
infile.getline(tmpLast, 31);
infile.getline(tmpPhone,15);
infile.getline(tmpId,10);
infile.getline(tmpGender, 1);

}

I need something to read in fields like that but read them all from one line. The format of the input file is like this:

HEUSER TRACEY 3117622694 134321 M
RAMSBURG MCKINLEY 2640367707 135994 M
CHOUINARD NARCISA 9798195332 397412 F
PARISEAU BURT 2640444897 590617 M
FULMER RAELENE 2519542441 657893 F
MCGOWIN JULI 4123600135 544137 F
BOULAY ANJELICA 6360431978 226930 F
BUBLITZ KASEY 3531285948 035945 M
HOCKENSMITH DANA 8108996190 470636 M

Where the last name is first, then first name, phone, ID#, and gender respectively. If someone can help me out with a function or direct me to somewhere that can assisr it would be greatly appreciated!

Thank you
dave120 is offline   Reply With Quote
Old 09-27-2006, 01:05 PM   #2 (permalink)
toe_cutter
Code Monkey
 
Join Date: Aug 2002
Location: Boston, MA
Posts: 79
toe_cutter is on a distinguished road
Send a message via ICQ to toe_cutter Send a message via AIM to toe_cutter Send a message via Yahoo to toe_cutter
Do a search on open and read system calls. If you need more help after looking them up, I will be glad to help.

Toe
__________________
toe_cutter is offline   Reply With Quote
Old 09-27-2006, 06:19 PM   #3 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
A little starter. Also, search this forum. Plenty of nice examples.
Code:
#include <iostream>
#include <string>
#include <fstream>
#include <vector>

struct Persons
{
  std::string sLastName;
  std::string sFirstName;
  unsigned int nBigNumber;
  unsigned int nSmallNumber;
  char letter;
};

int main()
{ 
  std::vector<Persons> vecPersons;
  Persons SpecialPersons;
  std::ifstream theFile("thefile.txt");

  if(theFile)
  {
    while(theFile >> SpecialPersons.sLastName >> SpecialPersons.sFirstName >>
      SpecialPersons.nBigNumber >> SpecialPersons.nSmallNumber >>
      SpecialPersons.letter)
    {
      vecPersons.push_back(SpecialPersons);
    }
  }
  else
  {
    std::cout<<"Error opening file... Program aborted!"<<std::endl;
    theFile.close();
    exit(-1);
  }

  //Just one example to demonstrate access to the vector and it's data.
  std::cout<<vecPersons[0].sLastName<<" "<<vecPersons[0].sLastName<<
    " "<<vecPersons[0].nBigNumber<<" "<<vecPersons[0].nSmallNumber<<
    " "<<vecPersons[0].letter<<std::endl;

  return 0;
}
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
file types: reading and writing pre_wreck Standard C, C++ 2 09-26-2005 05:49 AM
Reading in a file jello Standard C, C++ 4 05-04-2004 07:53 AM
Binary I/O file reading (0x1a trouble) Danish Standard C, C++ 2 05-26-2003 10:02 AM


All times are GMT -8. The time now is 05:56 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting