Total members 11889 |It is currently Thu Mar 28, 2024 11:51 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





I have a Books.txt file which looks like this.

Collapse | Copy Code
Book name---------------Author
Lord of the rings---------J. R R Tolkiens
Tom sawyer--------------Mark twain
Just after sunset---------Stephen king
The shining--------------Stephen king
Oliver twist------------Charles Dickens
Huck Finn---------------Marktwain
Children on Hurin------J R R Tolkiens
Silmarilions------------J R R tolkiens

How can i make my c++ program to read this file, read records as arrays, sort them, and search either by book name or by the auhtor. If i write Tolkiends, how can i make it output all the books written by J R R tolkiens?




Author:

Code:

std
::ifstream file("fileName");
std::string   line;

while(
std::getline(file, line))
{
    std::stringstream   linestream(line);
    std::string         data;
    std::string                strOne;
    std::string                strTwo;

    // If you have truly tab delimited data use getline() with third parameter.
    // If your data is just white space separated data
    // then the operator >> will do (it reads a space separated word into a string).
    std::getline(linestream, data, '\t');  // read up-to the first tab (discard tab).

    // Read the integers using the operator >>
    linestream >> strOne>> strTwo;
}
 


Code:
#include <iostream>
using std::cerr;
using std::cout;
using std::endl;
#include <fstream>
using std::ifstream;
#include <cstdlib> // for exit function
// This program reads values from the file 'text.txt'
// and echoes them to the display until a negative value is read.
int main()
{
   
ifstream indata// indata is like cin
   
int readNum// variable for input value
  
indata.open("text.txt"); // opens the file
   
if(!indata) { // file couldn't be opened
      
cerr << "Error: file could not be opened" << endl;
      exit(
1);
   }
  
indata >> readNum;
   while ( !
indata.eof() ) { //until end-of-file
      
cout << "Value is " << readNum<< endl;
      
indata >> readNum// sets EOF flag if no value found
   
}
   
indata.close();
   
cout << "Finished Reading" << endl;
   return 
0;
}
 


_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 2 posts ] 

  Related Posts  to : Search records from text file
 search for coding java/jsp for translation text     -  
 Create a text file     -  
 Import text file with ASP.NET     -  
 A simple search page using Google AJAX Search API     -  
 Reading and Writing To text file     -  
 display the content of text file     -  
 Read Arabic text from file     -  
 the text file i saved all in one line. How can i solve it?     -  
 uploading text file to java program     -  
 Server Names Records     -  



Topic Tags

C++ Files and I/O
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com