Solved

read from text file and rid white space

Posted on 2010-11-17
1
448 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
squareUp  challenge 22 177
Choosing the right language for new project 8 66
wordcount challenge 11 145
Problem to App 4 118
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

749 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