Bolian
Bolian

Reputation: 61

How to name text file at a certain directory?

Ok thanks for the answer Wug! I changed my code but now it's complaining about:

no matching function for call to

std::basic_ofstream::basic_ofstream(std::basic_string)

I'm not sure it makes any difference but i'll just post all of my code it's not that much so far. I'll try to keep it cleaner from now on.

#include <iostream>
#include <windows.h>
#include <direct.h>
#include <fstream>
using namespace std;

int main()
{   /*Introduction*/
SetConsoleTitle("Journal");

string action, prom0, filename, filepath;
filepath = "C:\\Users\\-\\Desktop\\Projects\\Journal Project\\Logs\\";
cout << "Hi and welcome to Journal! \nHere you can write down your day.\nWrite help for";
cout << "more \nType command to start: ";
/*Choose Action*/
cin >> action;
if (action == "new")
{system("cls");
/*Make new Journal file*/
cout << "Filename: ";
getline(cin, filename);
 mkdir("C:\\Users\\-\\Desktop\\Projects\\Journal Project\\Logs");
ofstream journallogs(filepath + filename);
journallogs.close();
}
else {
cout << "Wrong command\n";
};
return 0;}

Upvotes: 0

Views: 65

Answers (2)

Beta
Beta

Reputation: 99094

First, learn this:

  1. Start small and simple.
  2. Add complexity a little at a time.
  3. Test at every step.
  4. develop new functionality in isolation.
  5. Never add to code that doesn't work.

For the rest, I don't use Windows, so I can't be certain my code will work there, but the approach will.

You are trying to 1) get a filename from the user, 2) modify it and then 3) use it to open a file; we will develop these three things in isolation.

Getting a filename from the user. Civilized filenames do not contain whitespace, so they can be read with cin, but if you want to allow whitespace you can use getline instead. Either way, test it.

Modifying the filename. Write code that assigns a value to the filename, just as it does to the path-- do not get the filename from the user, it slows down your testing and is not proper isolation. Now try to append them. If you try filepath + filename, you may get a compiler error. Here's where you must understand the difference between std::string and char[]. A char[] is an array of char, and it (usually) contains a null-terminated sequence of characters; you must read up on arrays and pointers. It is a primitive type, and you cannot simply concatenate two of them with '+', you must use something like strcat, which is dangerous if you haven't done your homework on arrays. On the other hand, std::string is more sophisticated, and can handle '+' and many other operations. If you have a std::string x and you decide you want a char[] after all, you can get one like so: x.c_str().

Opening the file. If I remember right, the ofstream constructor can take a char[], but not a std::string. Test this with a hard-coded string (isolation!).

Once you have these three components working independently, you can hook them together.

Upvotes: 0

Wug
Wug

Reputation: 13196

There are 2 things wrong. The first is what the compiler's complaining about:

ofstream journallogs("C:\\Users\\-\\Desktop\\Projects\\Journal Project\\Logs\\" + getline(cin, filename), ios::out);

std::getline(istream&, string&) returns istream&, and you can't add char * to istream. I recommend taking a look at the documentation for getline(), which might help you understand better how you're supposed to use it. Here's an example anyway:

string filepath = "C:\\Users\\-\\Desktop\\Projects\\Journal Project\\Logs\\";
string filename;
getline(cin, filename);
ofstream journallogs(filepath + filename);

The second problem is that you're reading from cin into filename before calling getline(). When you call getline(), any contents of filename are dropped, so you'll effectively trim the first word off of your filename, which probably isn't what you want. To fix that, remove the extraneous cin >> filename;

Note: indentation is important and helps you read your own code. Put forth the effort to keep your code looking nice.

Upvotes: 1

Related Questions