[try Beta version]
Not logged in

 
 
Why my code cant read CPP file?

Nov 25, 2020 at 5:14am
One of my homework is to read a file and delete the extra spaces, and then output to a new file, My professor asked us to use CPP files. My code can read other types of files and can also output to CPP files, but it just can’t read CPP files.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
 #include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
void removeSpaces(ifstream& in_stream, ofstream& out_stream);

int main()
{


   //create two instance of the file
    ifstream inFile;
    ofstream outFile;
    inFile.open("oldfile.cpp");
    if (inFile.fail())
    {
        cout << "Input file did not open please check it and try again\n";
        exit(1);
    }
    outFile.open("newfile.cpp");
    if (outFile.fail())
    {
        cout << "Output file did not open please check it and try again\n";
        exit(1);
    }
    removeSpaces(inFile, outFile);
    cout<<"Extra blank spaces removed"<<endl;
    cout<<"Successful."<<endl;
    inFile.close( );
    outFile.close( );
    cout << "Done.\n";

    system("pause");
    return 0;
}
//Remove extra blank spaces from the file
void removeSpaces(ifstream& in_stream, ofstream& out_stream)
{
    char next;
    while (in_stream.get(next))
    {
        if (!isspace(next))
        {
                    out_stream <<next;
        }
        else
        {
            out_stream <<' ';
            while(isspace(next))
            in_stream.get(next);
            out_stream <<next;
        }
    }
}
Nov 25, 2020 at 5:19am
Are you saying that your process is printing "Input file did not open please check it and try again"?

See: https://cplusplus.com/forum/beginner/274350/#msg1184070

My code can read other types of files
The extension of the filename doesn't matter. Most likely, the file that you are trying to open doesn't exist in the place where your program is running (its "working directory").
Last edited on Nov 25, 2020 at 5:25am
Nov 25, 2020 at 6:48am
The extension of the filename doesn't matter. Most likely, the file that you are trying to open doesn't exist in the place where your program is running (its "working directory").

This file exists. Generally, it will appear when the program runs successfully:

Extra blank spaces removed
Successful
Done

This text appears in other types of files, but the input CPP file does not, and the program window is blank without any information.
Nov 25, 2020 at 8:21am
Try changing

 
if (inFile.fail())


to

 
if (!inFile.is_open())


Also, removeSpaces() may have an issue depending upon requirement. isspace() returns true if the char is a white-space char, not just a space. So space, tab and newline are all considered as white-space and returns true. So multiple spaces are converted to one space - but also multiple tabs and multiple newlines are also converted to a space.

Instead of

 
 out_stream <<' ';


have

 
out_stream << next;

Last edited on Nov 25, 2020 at 9:28am
Nov 25, 2020 at 6:10pm
Emmmm, Still not working
Nov 25, 2020 at 6:28pm
I don't understand your description of the problem.
Please link or paste an example of oldfile.cpp, and show what you expect newfile.cpp to look like.
Use tags to preserve whitespace.
Nov 25, 2020 at 6:32pm
1
2
       while(isspace(next))
            in_stream.get(next);


If in_stream.get(next); fails, this will loop forever.
Last edited on Nov 25, 2020 at 6:32pm
Nov 25, 2020 at 11:28pm
If in_stream.get(next); fails, this will loop forever.


What should I do? Delete it?

Please link or paste an example of oldfile.cpp, and show what you expect newfile.cpp to look like.


oldfile.cpp example is:

1
2
3
4
5
6
7
8
9
#include <iostream>
using     namespace   std;

int main()
{
  cout<< "HELLO      WORLD";
  return 0;
}


When the program is successful, newfile.cpp will be

1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;

int main()
{
 cout<< "HELLO WORLD";
 return 0;
}

Last edited on Nov 25, 2020 at 11:29pm
Nov 26, 2020 at 4:41am
Note what has already been said -- newlines also count as whitespace, so your program is eating any newlines it receives. Your newfile.cpp currently won't have newlines in it.

You could add an exception for newlines by doing:
if (!isspace(next) || next == '\n') instead of just !isspace(next).

What should I do? Delete it?
There are several ways you could clean this up, but the simplest addition would be to also check if the call to in_stream.get was successful, and it if wasn't, stop looping.

e.g.
while (isspace(next) && in_stream.get(next)) { };
Last edited on Nov 26, 2020 at 4:43am
Nov 26, 2020 at 5:28am
It turned out to be like this, I understand, thank you
Topic archived. No new replies allowed.