GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rating: Thread Rating: 2 votes, 5.00 average.
  #1  
Old 16-May-2008, 06:11
shahidrasul shahidrasul is offline
New Member
 
Join Date: May 2008
Posts: 1
shahidrasul is on a distinguished road

How To Read Integer From File In C++?


How To Read Integer From File In C++ Classes?
  #2  
Old 16-May-2008, 07:37
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,153
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How To Read Integer From File In C++?


Quote:
Originally Posted by shahidrasul
How To Read Integer From File In C++ Classes?
1. You read from a file in a C++ class the same way that you read it from anywhere else.

2. There is no built-in data type "Integer"

3. If the class has its own typedef for Integer, that is not an int and you are overloading the ">>" operator, then you can still use fundamental read operations on whatever data variables you have defined.

CPP / C++ / C Code:
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    int i;
    char *inname = "test.txt";
    ifstream infile(inname);

    if (!infile) {
        cout << "There was a problem opening file "
             << inname
             << " for reading."
             << endl;
        return 0;
    }
    cout << "Opened " << inname << " for reading." << endl;
    while (infile >> i) {
        cout << "Value from file is " << i << endl;
    }
    return 0;
}

File test.txt contains:
Code:
33 42 99 -1 123 <---That was the last one

Here's the output:
Code:
Opened test.txt for reading. Value from file is 33 Value from file is 42 Value from file is 99 Value from file is -1 Value from file is 123

Regards,

Dave
  #3  
Old 02-Jun-2008, 00:38
Archer Archer is offline
Junior Member
 
Join Date: May 2008
Posts: 31
Archer is on a distinguished road

Re: How To Read Integer From File In C++?


And what if one has to extract numbers in a file that contains array of characters and spaces,special characters ????

example of file sample.dat :

What is this contribution for?”… 200?? 500?? 1000??“Now I understood the cause”…”How much should be ok?”…”Could not give my share…. 79999 or 8000.

Can I send the contributions separately?” May be 15000.... “Yes, we must give back to society in some or the other way.” And so on…. 119033
  #4  
Old 02-Jun-2008, 08:17
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,153
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How To Read Integer From File In C++?


Quote:
Originally Posted by Archer
And what if one has to extract numbers in a file that contains

Here's the way I might approach the problem:

1. Define exactly what the file contains.

2. Define exactly what you are going to read from the file; that is, what are you going to do with whatever numbers you extract.

Assume, for the moment that it is a text file (lines separated by "newline" chars):

3. Make a loop that repeats the following two steps as long as there is more stuff that you need to read:

4. Read a line at a time into a std::string with getline().

5. Implement a loop that steps through the chars of the line looking for whatever numbers that you are going to read. Read each numeric item into whatever variable you have defined to hold the data.

Note, particularly, that if you start writing code before completing steps 1 and 2 (very explicitly and very precisely), you will probably be wasting your time. However, it's your time; waste it if you want to.


Regards,

Dave
  #5  
Old 02-Jun-2008, 09:24
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 592
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: How To Read Integer From File In C++?


Quote:
Originally Posted by Archer
And what if one has to extract numbers in a file that contains...
It sounds like you are fundamentally asking how to deal with mixed contents. As davekw7x states, it is important to be able to discern what delimits the various fields. The following may help illustrate:
CPP / C++ / C Code:
#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

int
main() {
    ifstream fin("example.txt");
    string line;

    while (getline(fin, line)) {
        cout << line << endl;

        istringstream tokenizer(line);
        string token;

        getline(tokenizer, token, ',');
        istringstream float_iss(token);
        float f;
        float_iss >> f;
        cout << f << endl;

        getline(tokenizer, token, ',');
        cout << token << endl;

        getline(tokenizer, token, ',');
        istringstream int_iss(token);
        int i;
        int_iss >> i;
        cout << i << endl;
    }
 
    return 0;
}
Given the comma-delimited contents of example.txt as:
Code:
3.14,huey,0 2.71,dewey,1 0.557,louie,2
Output will be:
Code:
3.14,huey,0 3.14 huey 0 2.71,dewey,1 2.71 dewey 1 0.557,louie,2 0.557 louie 2
  #6  
Old 03-Jun-2008, 00:04
Archer Archer is offline
Junior Member
 
Join Date: May 2008
Posts: 31
Archer is on a distinguished road

Re: How To Read Integer From File In C++?


Yes ocicat you got my point.Thanks buddy!!
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
Re: Things to Avoid in C/C++ -- gets() , Part 1 WaltP C Programming Language 5 21-Jun-2007 12:13
read integer form text file sarhang C++ Forum 1 15-May-2006 14:51
After execution - Error cannot locate /Skin File? WSCH C++ Forum 1 05-Mar-2005 21:03
How to detect end of file with read() function call? nkhambal C Programming Language 6 12-Oct-2004 01:08

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 13:24.


vBulletin, Copyright © 2000 - 2014, Jelsoft Enterprises Ltd.