Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

reading files


  • Please log in to reply

#1
Kevin39

Kevin39

    Member

  • Member
  • PipPip
  • 18 posts
i am creating a program for my C++ class that requires reading a text file of data and displaying one line based on the first character of the line. i.e. Computer systems listed starting at number 1.

Here is the code i have so far. The program reads lines 1 and 2 but not 3. I only want it to display line number 1 or number 2 or number 3 or any number in the list and the associated data on that line and only on that line.


/*This program retreives computer data from a file.
*
*Input: The computer terminal number.
*Output: All information about the Computer.
*****************************************************/
#include <iostream> //cin, cout, <, >
#include <fstream> //ifstream, ofstream
#include <string> //string, getline()
#include <cassert> //assert()

using namespace std;

int i;
string lineofText;

int main()
{
ifstream inStream;
inStream.open("Computerlist.txt"); //Open file for reading
assert( inStream.is_open()); //verifies the file is open

if (inStream.fail()) //If opening the file fails
cerr << "Error! Couldn't open " << "Computerlist.txt" << " file for reading ";

cout << "Enter the computer terminal number: ";
cin >> i;

for (;:tazz:
{
getline(inStream, lineofText);

if ( inStream.eof() ) break;
cout << lineofText;
}

}

any good or bad comments appreciated.

Thank you.
  • 0

Advertisements


#2
Swandog46

Swandog46

    Malware Expert

  • Member
  • PipPipPipPip
  • 1,026 posts
  • MVP
Hi Kevin39 :tazz:

A few problems. First of all, what do you want this to do exactly? What it does as of now is the following:

1) Asks for a 'terminal number', whatever that means, and then whatever you give it, it throws away the data.
2) Reads the file Computerlist.txt and prints out the entire file, minus the line feeds, except for the last line

For one thing, this line:

if (inStream.fail()) //If opening the file fails

will never actually get called, because if opening the file fails, the assert macro will kill your program before this test is ever made. So that's not needed. (or remove the assert)

Also, if you want the last line to be printed out, you should switch the order of these two lines:

if ( inStream.eof() ) break;
cout << lineofText;

That way it won't break before printing the last line of text.

Also, do you *want* it to strip line feeds (newlines)? If not, change this line:

cout << lineofText;

to this:

cout << lineofText << endl;

But most importantly, what do you really want this program to do? It's hard to know how to help otherwise. :) Hope this helps some...
  • 0

#3
Kevin39

Kevin39

    Member

  • Topic Starter
  • Member
  • PipPip
  • 18 posts
The program is only suppose to read the line associated with the Computer number the operator enters in the COUT line that states "Enter the computer number" The Text file associated with the program has the list of computers by number and their associated information that the program should display. The program reads the Txt file and extracts the line of information pertaining to the number of the computer number from the text file.
  • 0

#4
Swandog46

Swandog46

    Malware Expert

  • Member
  • PipPipPipPip
  • 1,026 posts
  • MVP
I see. We can easily adapt your current code to do this. I added comments where I changed things:



/*This program retreives computer data from a file.
*
*Input: The computer terminal number.
*Output: All information about the Computer.
*****************************************************/
#include <iostream> //cin, cout, <, >
#include <fstream> //ifstream, ofstream
#include <string> //string, getline()

// assert removed, so no need to #include <cassert>

using namespace std;



int main()
{


// no reason these variables shouldn't be local. Avoid global variables whereever possible! :)
int i;
string lineofText;
int counter; // added by Swandog46


ifstream inStream;

inStream.open("Computerlist.txt"); //Open file for reading

if (inStream.fail()) //If opening the file fails
{
cerr << "Error! Couldn't open " << "Computerlist.txt" << " file for reading ";
exit(1); // added by Swandog46, to replace the assert macro
}




cout << "Enter the computer terminal number: ";
cin >> i;

counter = 1; // added by Swandog46. Note that I initialize counter to 1 because I am assuming that you want the first line of the text file to be considered line 1, as opposed, for example, to line 0.

// I would probably also replace the infinite for loop with a for loop that contains a test (i.e. a non-infinite loop), but that's mainly a point of style so I'll leave it your way :)

for (;:tazz:
{
getline(inStream, lineofText);

if(counter == i) cout << lineofText;

if ( inStream.eof() ) break;

counter++;

}

// don't forget to close the file!
inStream.close(); // added by Swandog46

return 0; // don't forget the return statement (in unix, for example, return 0 signifies program success)

}

I *think* this is right.
Hope this helps :woot:
  • 0

#5
Kevin39

Kevin39

    Member

  • Topic Starter
  • Member
  • PipPip
  • 18 posts
Thank you so much, it works as designed. i appreciate your assistance in this program.

Kevin
  • 0

#6
Swandog46

Swandog46

    Malware Expert

  • Member
  • PipPipPipPip
  • 1,026 posts
  • MVP
No problem. You should also note that there is no error checking as it stands now, so lots of things could go wrong. In particular, suppose I were to enter the number negative one at the prompt for a line number. The program would not print anything out at all. Suppose I entered "hi" --- who knows what would happen? Just something to think about :tazz:
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP