Previous in Forum: Omni Flow Computer Malfunction   Next in Forum: Cleaning Up a Computer
Close
Close
Close
6 comments
Member

Join Date: Feb 2013
Posts: 8

C++ Displaying a Text File...("Echo" a Text File)

07/18/2013 11:38 PM

So I'm really stuck trying to figured this bug on the program that is preventing me from displaying the text of my program..

#include <iostream>

#include <iomanip>

#include <fstream>

#include <sstream>

#include <string>

#include <stdio.h>

using namespace std;

int main ()

{

ifstream infile;

ofstream offile;

char text[1024];

cout <<"Please enter the name of the file: \n";

cin >> text;

infile.open(text);

string scores; // this lines...

getline(infile, scores, '\0'); // is what I'm using...

cout << scores << endl; // to display the file...

// Is there any way cleaner or simple method i can used to display the file ?

//All the method in the internet are difficult to understand or keep track due to

// the file is open in loop..

//I want a program that you type the name of the file and displays the file

// the file will contain the following...

// jack 23

//smith 27

//also I need to obtain data from the file

//now I'm using the following code to obtain that information from the file...

string name1;

int name2;

string name3;

int name4;

infile >> name1;

infile >> name2;

infile >> name3;

infile >> name4;

cout << "these two individual with their age add are" << name2 + name4 <<endl;

// 23 + 27

//the result I get is a bunch of numbers...

return 0;

}

Register to Reply
Interested in this topic? By joining CR4 you can "subscribe" to
this discussion and receive notification when new comments are added.

Comments rated to be Good Answers:

These comments received enough positive ratings to make them "good answers".
Guru

Join Date: Mar 2012
Location: Out of your mind! Not in sight!
Posts: 4425
Good Answers: 107
#1

Re: C++ displaying a text file...("echo" a text file)

07/19/2013 1:53 AM

is "cout" correct or do you need a "count" there?

Looks dodgy!

Otherswise: No Idea!

__________________
Common Sense Dictates
Register to Reply
Guru
Popular Science - Weaponology - New Member Netherlands - Member - New Member Fans of Old Computers - Commodore 64 - New Member

Join Date: Sep 2007
Location: Japan
Posts: 2703
Good Answers: 38
#4
In reply to #1

Re: C++ displaying a text file...("echo" a text file)

07/21/2013 9:01 PM
__________________
From the Movie "The Big Lebowski" Don't pee on the carpet man!
Register to Reply Off Topic (Score 4)
Guru

Join Date: Mar 2012
Location: Out of your mind! Not in sight!
Posts: 4425
Good Answers: 107
#5
In reply to #4

Re: C++ displaying a text file...("echo" a text file)

07/22/2013 8:43 AM

Cheers! Long since I had to look at these things and never done C-plus. Good reference page. Why did you put this Off Topic?

__________________
Common Sense Dictates
Register to Reply Off Topic (Score 5)
Guru
Popular Science - Weaponology - New Member Netherlands - Member - New Member Fans of Old Computers - Commodore 64 - New Member

Join Date: Sep 2007
Location: Japan
Posts: 2703
Good Answers: 38
#6
In reply to #5

Re: C++ displaying a text file...("echo" a text file)

07/22/2013 8:34 PM

Bcuz it was your question and not the the thread owner's question.

__________________
From the Movie "The Big Lebowski" Don't pee on the carpet man!
Register to Reply Off Topic (Score 5)
2
Guru
Popular Science - Weaponology - New Member Netherlands - Member - New Member Fans of Old Computers - Commodore 64 - New Member

Join Date: Sep 2007
Location: Japan
Posts: 2703
Good Answers: 38
#2

Re: C++ displaying a text file...("echo" a text file)

07/19/2013 3:02 AM

This could be better asked on a C++ forum,

What is the location of your file?

I see no code that catches errors

where do you want to display the file? on the console?

Search the web there are many C(++,#)

Download Visual C from Microsoft it is vary useful (or eclipse)

__________________
From the Movie "The Big Lebowski" Don't pee on the carpet man!
Register to Reply Good Answer (Score 2)
Guru

Join Date: Oct 2009
Posts: 1443
Good Answers: 30
#3

Re: C++ Displaying a Text File...("Echo" a Text File)

07/20/2013 10:38 AM

Several problems here. First, the line
string scores;

allocates space for the object, but does not initialise it. You need to rewrite as
string scores = new string;

Second, your code reads the entire text file into a single string object. This is legal, but it does not help when you want to process individual lines. I prefer to read individual lines into an array of strings, or even a linked list of strings, identified by an index, but you can do the same from your big string by using an iterator to pick up individual characters:
string singleLine = ""; // another way of initialising
string::iterator iter = scores.begin(); // start of string
while (iter != scores.end()) // still characters to be extracted
{
while (iter != '\n') // end of line marker
{
singleLine += *iter++; //add single character and move iterator
}
// process a string "Jim 37" here
}// move on to next line

Register to Reply
Register to Reply 6 comments
Interested in this topic? By joining CR4 you can "subscribe" to
this discussion and receive notification when new comments are added.

Comments rated to be Good Answers:

These comments received enough positive ratings to make them "good answers".
Copy to Clipboard

Users who posted comments:

Epke (3); IdeaSmith (2); phph001 (1)

Previous in Forum: Omni Flow Computer Malfunction   Next in Forum: Cleaning Up a Computer

Advertisement