-1

I tried programming a file writer, but when i try to write to a file with something that has multiple words it will suddenly create files.

My code

#include <fstream>
#include <iostream>
#include <unistd.h>

int main(int argc, char *argv[]) {
  char cwd[256];

  while (true) {
    getcwd(cwd, 256);
    std::string cwd_s = (std::string)cwd;
    std::string Input;

    std::cout << cwd_s << "> ";
    std::cin >> Input;
    std::ofstream file(Input);

    std::cout << "cmd /";
    std::cin >> Input;
    file << Input;
  };

  for (int i; i < argc; i++) {
    std::cout << argv[i] << '\n';
  };

  return 0;
}

I expected to get this:

C:\Users\code> File.txt
cmd /hello world!

File.txt
hello world!

But it only had "hello", it created another file named world!

I have tried changing the code, but to no avail.

kiner_shah
  • 3,939
  • 7
  • 23
  • 37
  • 1
    The stream extract operator `>>` reads ***space-delimited*** words. If you want to get the whole line, space an all, you need to use `std::getline`. – Some programmer dude Feb 17 '23 at 10:23
  • 1
    Also note that the cast in `(std::string)cwd` is not needed. In fact, in almost all cases where you feel the need to do a C-style cast like that, you should take it as a sign that you're probably doing something wrong. – Some programmer dude Feb 17 '23 at 10:23
  • You also have a problem here `for (int i; ` where `i` has no start value for the loop. – BoP Feb 17 '23 at 10:29
  • Have a look at https://stackoverflow.com/questions/5838711/stdcin-input-with-spaces. – Jorge López Feb 17 '23 at 10:48

1 Answers1

0

So I have wrote this code that I think does what you expect. The behavior you were seing is because you used the same string to store the filename and the user input. Also you redefined a new file every loop (without closing the previous one). I added a signal handler since if you press Ctrl+C the program would quit without saving/closing the file. I added comments about how you can make a better CLI interface (if you're interested)

#include <iostream>
#include <fstream>
#include <string>
#include <unistd.h>

std::ofstream outfile;

void signalHandler(int signum) {
    outfile.close();
    exit(signum);
}

int main() {
    char cwd[256];
    if (getcwd(cwd, sizeof(cwd)) != NULL) {
        std::cout << cwd << "> ";
    } else {
        std::cerr << "Error: Could not get current working directory." << std::endl;
        return 1;
    }

    std::string filename;
    std::getline(std::cin, filename);

    outfile.open(filename);
    // We intercept the Ctrl+C signal to close the file before exiting. Else nothing will be written to it.
    // You can also use Ctrl+D (EOF: End Of File) to exit the program.
    // The best praticte would be to implement a command line interface with a "quit" command. (like a map<string, function> for example)
    signal(SIGINT, signalHandler);

    // Another good practice is to check if the file did open correctly.
    if (!outfile.is_open()) {
        std::cerr << "Error: Could not open file for writing." << std::endl;
        return 1;
    }

    std::cout << "cmd / ";
    char ch;
    while (std::cin.get(ch)) {
        outfile.put(ch);
        if (ch == '\n') {
            std::cout << "cmd / ";
        }
    }

    outfile.close();
    return 0;
}

Hope it will help you ! And if you have any question about the code feel free to ask I'll explain !

Nnevalti
  • 16
  • 3