im very confused and would like some help with files

So I have this homework that is due and I really have no idea how to go about doing this. Ideally some links to explaining what to do would be the most helpful so that I can actually learn how to do this but if someone just writes the program, that would work too. The question is:

1. Create a complete program that:
a. prompts a user to enter the name of an existing file.
b. If the file does not exist and or cannot be opened, the program should display an error message and quit.
c. If the file exists and can be opened, prompt the user for a new file-name.
d. If the new file name already exists, print a message telling the user that the file already exists and ask the user whether they want to APPEND
i. if the user enters ‘N’ for “NO”, print a message “Operation canceled by user” and quit
ii. otherwise, open the file for APPEND and copy all of the contents from the original file
e. If the file doesn’t already exist, create a new file with the name supplied by the user and copy the contents of the original file into it.
but if someone just writes the program, that would work too


bet that would, wouldn't it?
"ideally some links explaining what to do would be most helpful"

thanks for the valid input and not wasting my time
Have you started writing any of the code? If so post it and we can go from their.
Last edited on
string fileName = "";
string newFileName = "";

ofstream outFile;
outFile.open("staff.txt");
outFile << "joe bill alex";
outFile.close();

cout << "Enter the name of an existing file to open it" << endl;
cin >> fileName;

if (fileName == "staff.txt") {
cout << "Enter new file name" << endl;
cin >> newFileName;
}
else {
cout << "File does not exist" << endl;
}

if (newFileName == )

This is what I have so far, I don't know how to determine if the new file name exists and everything that follows.
Did you open a new file to check if it exists?
I don't know to be honest, everything I posted above is what I have.
Compiled and tested:

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
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <string>
#include <fstream>

int main()
{
	std::cout << "Enter exisiting file name: " << std::flush;
	std::string fileName;
	std::getline(std::cin, fileName);
	std::ifstream originalFile(fileName);
	if (!originalFile.is_open())
	{
		std::cout << "Error: File does not exist or cannot be opened.\n";
		std::cin.get(); // pause
		return 0;
	}
	else
	{
	        std::string text, line;
		std::cout << "Enter new file name: " << std::flush;
		std::getline(std::cin, fileName);
		std::ifstream file(fileName);
		if (file.is_open())
		{
		        file.close();
			std::cout << "File name exists. Do you wish to append (y or n): " <<
                        std::flush;
			char append;
			std::cin >> append;
			if (append == 'n')
			{
				std::cout << "Operation canceled by user.\n";
				std::cin.get(); // pause
			}
			else
			{
			    std::ofstream fileToAppend(fileName, std::ios::app);
                            text += "\n";
			    while (std::getline(originalFile, line))
			    {
			        text += line + "\n";
			    }
			    fileToAppend << text;
			    fileToAppend.close();
			    std::cout << "Finished appending to file.\n";
			}
		}
		else
		{
		    file.close();
		    std::ofstream newFile;
		    newFile.open(fileName);
		    while (std::getline(originalFile, line))
		    {
		        text += line + "\n";
		    }
		    newFile << text;
		    newFile.close();
		    std::cout << "Finished copying to file\n";
		}
	}
	originalFile.close();
	std::cin.get(); // pause
	return 0;
}
Last edited on
Wow thank you so much. If you could, could you clarify your use of getline? For example, on line 21, is the cin, fileName what allows you to change the name of the existing file? And can you also explain your use of flush? Is that the same as endl; ?
Your welcome.

fileName is a string to store the name of the file the user wants to copy from. getline() allows you to take input with spaces too ("filename" vs. "file name").

endl is a manipulator that flushes the stream and prints a newline. flush only flushes the stream without printing a newline. I used it to make sure text was outputted to the screen.
Last edited on
Topic archived. No new replies allowed.