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.
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").
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.
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.
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.
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)) { };