Moving the input file with c++ - c++

I want to be able to enter any file in MoveFile() and it will move the file to this folder: C:\folder\fl.txt. When I enter MoveFileA("C:\\fl.txt", "C:\\folder\\fl.txt"); Then everything works, but I need to move the first file (the one that is fl.txt) to folder ...
How can this be implemented so as not to always enter the file name (C:\folder\fl.txt) so that it auto inputs it?
Here is my code:
#include <iostream>
#include <string>
#include <Windows.h>
#include <stdio.h>
#include <cstdlib>
using namespace std;
int main() {
MoveFileA("C:\\fl.txt", "C:\\folder\\fl.txt");
cout << "Operation Succesful" << endl;
cout << endl;
system("pause");
}
I need something like:
int main() {
int path_a;
cin >> path_a;
MoveFileA("path_a", "C:\\folder\\path_a");
cout << "Operation Succesful" << endl;
cout << endl;
system("pause");
}

Perhaps you are looking for something like this:
#include <iostream>
#include <string>
#include <cstdlib>
#include <Windows.h>
using namespace std;
int main() {
string filename;
cin >> filename;
if (MoveFileA(("C:\\"+filename).c_str(), ("C:\\folder\\"+filename).c_str()))
cout << "Operation Successful" << endl;
else
cout << "Operation Failed" << endl;
cout << endl;
system("pause");
}
Though, if you are using C++17 or later, you might consider a pure C++ solution using std::filesystem::rename() instead of the Windows-specific MoveFileA() function, eg:
#include <iostream>
#include <string>
#include <filesystem>
#include <system_error>
#include <cstdlib>
using namespace std;
namespace fs = std::filesystem;
int main() {
fs::path filename;
cin >> filename;
fs::path root("C:\\");
error_code ec;
fs::rename(root / filename, root / "folder" / filename, ec);
if (ec)
cout << "Operation Failed" << endl;
else
cout << "Operation Successful" << endl;
cout << endl;
system("pause");
}

Related

"'main': identifier not found" error in c++

I keep getting this error and have no idea how to fix it because I don't see anything wrong with my code.
#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
#define GREEN "\033[32m"
#define RED "\033[31m"
#define RESET "\033[0m"
void file_create(std::string name) {
std::ifstream file(name);
if (file.is_open()) {
file.close();
std::cout << "File already exists..." << std::endl;
main();
}
else {
file.close(); std::ofstream newFile(name);
if (newFile.is_open())
std::cout << GREEN "New file successfully created..." << RESET << std::endl;
else
std::cout << RED "File could not be created" << RESET << std::endl;
newFile.close();
}
}
int main() {
}
The main function is not intended to be invoked from your code. It is also a nonsense since it is called automatically when program starts. But if you need some alternative main to be invoked upon some condition, you can call it inside your main function
...
int alternative_main()
{
place your program there
}
...
void file_create(std::string name) {
...
if (file.is_open()) {
...
alternative_main()
}
else {
...
}
}
Suppose your main looks like this
int main() {
file_create("myfile");
}
#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
#define GREEN "\033[32m"
#define RED "\033[31m"
#define RESET "\033[0m"
int main(); // defined before file_create
void file_create(std::string name) {
std::ifstream file(name);
if (file.is_open()) {
file.close();
std::cout << "File already exists..." << std::endl;
main();
}
else {
file.close(); std::ofstream newFile(name);
if (newFile.is_open())
std::cout << GREEN "New file successfully created..." << RESET << std::endl;
else
std::cout << RED "File could not be created" << RESET << std::endl;
newFile.close();
}
}
int main() {
}
Define int main() above file_create() so you can call it in file_create
#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
using namespace std;
//use this instead of repetitively using std all the time
#define GREEN "\033[32m"
#define RED "\033[31m"
#define RESET "\033[0m"
void file_create(string name) {
ifstream file(name);
if (file.is_open()) {
file.close();
cout << "File already exists..." << endl;
return 0;
}
else {
file.close();
ofstream newFile(name);
if (newFile.is_open())
cout << GREEN "New file successfully created..." << RESET << endl;
else
cout << RED "File could not be created" << RESET << endl;
newFile.close();
}
}
int main() {
string name;
cin>>name;
file_create(name);
}
try using this way...

Creating new txt files with c++ program, naming with variables

I have created a program and I want to create with it files like aff1.txt, aff2.txt, etc. In these files, I want to have here a text created this way: It will open the file: text.txt and it will take each sentence, copy it 4700/sentence length times to each file. But it isn't working, when: cout << ss << endl;, it writes to cmd nothing, while there should be something, which was assigned before. What should I do?
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string>
#include <string.h>
#include <sstream>
using namespace std;
int main()
{
ifstream vstup("text.txt"); // 4700,2700,2200,1700
string vety;
getline(vstup,vety);
vstup.close();
string ss="affn.txt";
char q[vety.length()];
for (int u=0;u<vety.length();u++)
{
q[u] = vety[u];
}
int l=0,m=0,n=0;
int v,i,e,o;
char vl[999999];
//cout << vety.length() << endl;
for (i=0;i<vety.length();i++)
{
//cout << "ss" << endl;
if (q[i]=='.')
{
// cout << "ss" << endl;
v=4700/i;
for (e=0;e<v;e++)
{
//cout << "ss" << endl;
for (o=0;o<i-l;o++)
{
// cout << "ss" << endl;
m=o+e*(i-l);
vl[m]=q[o+l];
}
}
l=l+i;
cout << vl << endl;
n++;
//ofstream aff("aff.txt");
//aff << vl << endl;
//aff.close();
ss[3]=n;
ofstream writer(ss.c_str());
//writer.open(ss.c_str());
writer << vl << endl;
writer.close();
cout << ss << endl;
ss.clear();
}
}
return 0;
}

C++ function unable to another function and ends automaticly

So I was making a file editor using c++ and it has 3 functions and it needs to call each other to work properly.But When code tries to call other functions it end abnormly .
I tried changing the order of functions but it does nothing.It will compile properly without warnings
it needs output the contents of the file.
#include <iostream>
#include <fstream>
#include <bits/stdc++.h>
#include <string>
#include <iomanip>
#include <unistd.h>
#include <sstream>
using namespace std;/* std */
/* data */
char buffer;
std::string fname;
int reader(){
std::ifstream readfile;
readfile.open(fname.c_str());
readfile>>buffer;
std::cout << buffer<< '\n';
int write();
}
int options(){
cout << "************************"<< '\n';
cout << "* Starting File editor *"<< '\n';
cout << "************************"<< '\n';
cout << "* Enter Filename *"<< '\n';
cin >>fname;
cout << "Opening File"<<fname<< '\n';
int reader();
std::cout << buffer<< '\n';
}
int write(){
cout << "writing to file " << '\n';
std::ofstream writefile;
writefile.open(fname.c_str());
writefile<<buffer;
cout << "writing done " << '\n';
}
int main()
{
/* code */
options();
return 0;
}
options() is not calling reader(), and reader() is not calling write(). In both cases, you are simply declaring functions, not actually calling them.
int reader(){
...
int write(); // <-- a declaration, not a call!
}
int options(){
...
int reader(); // <-- a declaration, not a call!
...
}
int main() {
...
options(); // <-- a call, not a declaration!
..
}
Try this instead:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
/* data */
char buffer;
std::string fname;
int reader(){
cout << "opening file " << fname << '\n';
std::ifstream readfile(fname.c_str());
readfile >> buffer;
std::cout << buffer << '\n';
}
int write(){
cout << "writing to file " << '\n';
std::ofstream writefile(fname.c_str());
writefile << buffer;
cout << "writing done" << '\n';
}
int options(){
cout << "************************"<< '\n';
cout << "* Starting File editor *"<< '\n';
cout << "************************"<< '\n';
cout << "* Enter Filename *"<< '\n';
cin >> fname;
reader();
write();
}
int main() {
/* code */
options();
return 0;
}
In addition to the comments above about calling the functions, it seems like it would be good to initialize buffer as a char array as shown below:
#include <iostream>
#include <fstream>
//#include <bits/stdc++.h>
#include <string>
#include <iomanip>
#include <unistd.h>
#include <sstream>
using namespace std;/* std */
/* data */
char buffer[]{"Short test"};
std::string fname;
void write(){
cout << "writing to file " << '\n';
std::ofstream writefile;
writefile.open(fname.c_str());
writefile<<buffer;
cout << "writing done " << '\n';
}
void reader(){
std::ifstream readfile;
readfile.open(fname.c_str());
readfile>>buffer;
std::cout << buffer<< '\n';
write();
}
void options(){
cout << "************************"<< '\n';
cout << "* Starting File editor *"<< '\n';
cout << "************************"<< '\n';
cout << "* Enter Filename *"<< '\n';
cin >>fname;
cout << "Opening File"<<fname<< '\n';
reader();
std::cout << buffer<< '\n';
}
int main()
{
/* code */
options();
return 0;
}
You can declare functions(not compulsory in your case) after all #include statements like:
int reader();
int write();
int options();
You call write function as write(); reader function as reader();
Since functions are not returning anything you could change int reader() to void reader(), int write() to void write() and so on. Keep main as int main() though.

C++: Text file not being read

I'm trying to print string values from a text file to the console but the file isn't opening.
#include "stdafx.h"
#include <iostream>
#include <string>
#include<fstream>
int main()
{
ifstream nameFile;
nameFile.open("E:\\Names.txt");
string a;
int studentCount = 0;
if (nameFile.is_open())
{
while (getline(nameFile, a)) {
cout << a << '\n';
studentCount++;
}
cout << studentCount;
}
else
cerr << "Unable to load file" << endl;}

manipulating file using functions

Hi I am trying to write program that a program that allows the user to transfer an amount from an account (the account being text file "shop" which simply contains the value 100).
I want to have it so that the user can make as many transfers as they want, without overdrawing the account. The file also needs to be updated after each transaction. Can anyone help me with where I'm going wrong?
int read_balance(void);
void write_balance(int balance);
#include <limits>
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
int _tmain(int argc, _TCHAR * argv[])
{
std::cout << "You have choosen to transfer an amount" << std::endl;
std::cout << "How much do you wish to transfer from the shop account?" << std::endl;
int amount = 0;
if (std::cin >> amount)
{
std::cout << "DEBUG: amount:" << amount << "\n";
int balance = read_balance();
if (amount <= 0)
{
std::cout << "Amount must be positive\n";
}
else if (balance < amount)
{
std::cout << "Insufficient funds\n";
}
else
{
int new_balance = balance - amount;
write_balance(new_balance);
std::cout << "New account balance: " << new_balance << std::endl;
}
}
system("pause");
return 0;
}
int read_balance(void)
{
std::ifstream f;
f.exceptions(std::ios::failbit | std::ios::badbit);
f.open("shop.txt");
int balance;
f >> balance;
f.close();
return balance;
}
void write_balance(int balance)
{
std::ofstream f;
f.exceptions(std::ios::failbit | std::ios::badbit);
f.open("shop.txt");
f << balance;
f.close();
}
As the compiler warns you when using precompiled headers for stdafx.h the #include "stdafx.h" must be the first line of code. So it's better to start with
#include "stdafx.h"
#include <limits>
#include <iostream>
#include <fstream>
using namespace std;
int read_balance(void);
void write_balance(int balance);