Remove whitespace from file

I have pasted my .cpp code below, my goal is removing the whitespace from my .txt file. Is someone able to provide suggestions and help me figure this out?

#include <iostream>
#include <sstream>
using namespace std;
string solve(string s) {
   string answer = "", temp;
   stringstream ss;
   ss << s;
    while(!ss.eof()) {
      ss >> temp;
      answer+=temp;
     }
   }
   return answer;
}
int main() {
   char buffer[100];
   FILE *fp = fopen("input.txt", "r");
   while (fgets(buffer, sizeof(buffer), fp))
   {
     cout << solve(fp);
   }
   fclose(fp);
   return (0);
}

Doesn’t fgets put the string into that buffer variable? I would expect your function needs to take the buffered string and handle that. Also, not sure if you need to clear the buffer between reads.

Alternatively, can you use getline instead?

Sorry for replying to an old question.

Your approach is fine. But you need to either use const char *char_pointer or std::string in both reading and processing of text.

In main, you have used const char *char_pointer, while in string solve(string) there is a string parameter. They are not compatible.

Either change the string parameter in string solve(string) to FILE * (which is discouraged in C++) or read your file using ifstream object from fstream header.

Mosh has explained using fstream objects in Streams and Files module in part 2 of C++ course.

PS: Remove curly bracket from line 11 or 12