Having issues with the getting the string to read the location path ... please help im a bit new

#include <fstream>
#include <sstream>
#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
char ch, word=' ';
int ch_cnt=0, space_cnt=0, word_cnt=0;

//if( word >>'a');
//word_cnt--;
ifstream inFile; // Identify the file

string path;

cout<< "Enter the location of the file.";
cin>> path;

inFile.open("path");
// Associate the file name with a physical
// file on the disk.

if( inFile.fail() )
{
cout<<"Either you or the machine are having issues! The test results have\n"
"determinded that you are the error.\n";
getch();
exit(1);
}
// If we get here, the file is open


// THE BATCH PROCESSING LOOP
ch = inFile.get(); // Read

while( !inFile.eof() ) //Test for end of file
{
cout<<ch; // Process
ch_cnt++;

if( isspace(ch))
space_cnt++;

if( !isspace(word)&& isspace(ch))
word_cnt++;



word=ch;
ch=inFile.get(); //Read
}

if(!isspace(word))
word_cnt++;



cout<<"\n\n|"<<word<<"|\n\n";

cout<<"\n The character count is "<<ch_cnt;
cout<<"\n \n The space count is "<<space_cnt;
cout<<"\n \n The word count is "<<word_cnt;
inFile.close();


cout<<"\n\nNormal termination.\n";
getch();
return 0;
}
Are we trying to read a directory on the disk? Or are we trying to read a file named 'path'?

What you have done is tried to open a file named 'path'. It looks like I similar project I helped someone else on in this link, which you are showing some of the same logic problems.
http://www.cplusplus.com/forum/general/51305/

If I read the code a little further it should be:
 
inFile.open( path.c_str() ); // note the difference to inFile.open("path"); 


the other thing I would change is :
1
2
char ch, word=' ';
int ch_cnt=0, space_cnt=0, word_cnt=0;

to:
1
2
3
char ch;
string word;
int ch_cnt=0, space_cnt=0, word_cnt=0;


I don't need string stream for this problem.

hope that points you in a direction.
Last edited on
the idea behind my string was to allow people to type the path"directory" of the text file... i tired putting it the location I.E C:\test.txt but it resulted in an infile fail closing the program
well you need to access the contents of the string of path, and "path" is an indication of the file name "path", which you don't want. And that is what I gave you in my example.

if you followed some of the code examples and handled some of them differently I would see something like I give an example here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
string path;
char inchar;

cout<< "Enter the location of the file.";
cin>> path;

if( inFile.open( path.c_str() ) )
{
       while (!inFile.eof())  // watch for the end of file.
       {
              inFile >> inchar;  // read a character in.
              cout << inchar;  // print the character read in...
       }
}
else
{
       cout << "I couldn't open the file for some reason!!" << endl
}


I guess I would expect to see something like this pattern in your code to know if i had a problem with the file.
Topic archived. No new replies allowed.