Solved

read from text file and rid white space

Posted on 2010-11-17
1
450 Views
Last Modified: 2012-05-10
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
using std::cin;
using std::cout;
using std::string;
string display(string);
const int size=32;
char cword[size];
string word;
int arr[size]={0};
int x = 0, len=0;

int main ()
{
    string array[5]; // creates array to hold names
     
    short loop=0; //short for loop for input
	display(cword);
    string line; //this will contain the data read from the file
    ifstream myfile ("testfile1.txt"); //opening the file.
    if (myfile.is_open()) //if the file is open
    {
        while (! myfile.eof() ) //while the end of file is NOT reached
        {
         
           getline (myfile,line); //get one line from the file
          
           display(line);
          
            loop++;
             
        }
        myfile.close(); //closing the file
    }
    else cout << "Unable to open file"; //if the file is not open output

    system("PAUSE");
    return 0;
}
string display(string word) {
  
   len=word.length();
  
   for (x=0;x < len;x++) {    // While the string isn't at the end...
        
        if (int(word[x]!=40)){
       cout << word[x] ;//<< " : "<< int(word[x]);    // Display char and ascii
        
        arr[x]=int(word[x]);
                            }
           
    }
    
return word;
}

Open in new window

testfile1.txt
0
Comment
Question by:jaypappas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 11

Accepted Solution

by:
cup earned 500 total points
ID: 34161933
Which area are you having problems with?


You could use getch() or getchar() instead of system("PAUSE")
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This article will show, step by step, how to integrate R code into a R Sweave document
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question