Move file from one location to another location in window c++ [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Simple code for moving file to another location using window 10 visual studio c++ 2015. In window how to move file from one location to another.
I have a text file at location D:\data.txt . I want to change its loaction to C:\total data\data.txt .
#include <cstdio>
int main (void)
{
std::rename ("old_name", "new_name");
return 0;
}
I already use rename fuction for moving using but it not works further details https://bytes.com/topic/c/answers/132322-file-move-programmatically
Just i want to change location of file.

Changes the name of the file or directory specified by oldname to newname.
If oldname and newname specify different paths and this is supported by the system, the file is moved to the new location.
#include <stdio.h>
int main ()
{
int result;
char oldname[] ="D:\\data.txt";
char newname[] ="C:\\datadull\\newname.txt";
result= rename( oldname , newname );
if ( result == 0 )
puts ( "File successfully renamed" );
else
perror( "Error renaming file" );
return 0;
}

Related

How to write string to .txt file in C++ using FILE [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
I have string str = "6.5.1"
I want to write str to file .txt, but the result is ��j
Here my code
FILE *outfile = fopen("solution.txt", "w");
string test = "6.5.1";
fprintf(outfile, "%s\n", test);
I use string, FILE because I want to pass FILE as an argument, and convert string from another file to method.
How can I fix this problem?
Thanks.
p\s: sorry, tag is c++ not c
You clarified that you needed a c++ solution and required to use FILE *:
#include <cstdio>
#include <string>
int main(void) {
FILE *outfile = std::fopen("solution.txt", "w");
std::string test = "6.5.1";
std::fprintf(outfile, "%s\n", test.c_str());
std::fclose(outfile);
}

How can I use regex to determine the existence and size of groups of files in C/C++ on Linux? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a program on a Linux system that generates data log files with predictable file names that I need to access, but I have to do so through a C/C++ interface. The program accessing the data needs to be able to take a file path with a regex to specify certain file name ranges and do the following:
Determine if there are files matching the regex that exist
Determine the total size of all of the matching files
I'm using these as checks before I compress and transfer the files. How can I do this in C/C++?
You could do something like this:
#include <regex>
#include <string>
#include <experimental/filesystem>
// make using it sane
namespace fs = std::experimental::filesystem;
int main(int argc, char** argv)
{
// default to current directory
fs::path dir = argv[1] ? argv[1] : ".";
fs::directory_iterator dir_ent{dir};
fs::directory_iterator dir_end;
std::regex e{R"~(.*\.txt)~"};
std::smatch m;
decltype(fs::file_size("")) total_size = 0;
for(; dir_ent != dir_end; ++dir_ent)
{
// for some reason regex_match won't accept temporaries
std::string s = dir_ent->path().string();
if(!std::regex_match(s, m, e))
continue;
// deal with filepath here
total_size += fs::file_size(dir_ent->path());
// etc...
}
}

C++ command line argument verification [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
If provided with command line argument argv[], what is a way to determine whether or not that input is a file name.
E.g. If we are entering ex.txt into the command line, and printing out the contents, how can I write a conditional statement to determine whether the input for argv[1] is correct?
Thanks. Let me know if I was too vague. This is my first post, and english is not my first language.
You probably don't want to know only whether the file exists but also if you can open and read it. The only reliable way (And, by the way, the only way currently supported by standard C++.) to do this is to try opening the file and see if you succeed.
#include <fstream>
#include <iostream>
int
main(const int argc, const char *const *const argv)
{
for (int i = 1; i < argc; ++i)
{
std::ifstream istr {argv[i]};
if (!istr)
{
std::cerr << "error: " << argv[i] << ": cannot read file\n";
continue;
}
// Do something with the stream...
}
}
Be aware that if you close the file after verifying that it is good, there is no guarantee that it will still be good if you try to open it again later. Some other process could have deleted it or taken your permissions to read it.

Accessing data in a text file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How could I access a text file and go through word by word. I understand how to open the file but just not how to pull out each word one by one. I think it has something to do with arrays?
Simply:
#include <fstream>
#include <iostream>
int main()
{
std::fstream file("table1.txt");
std::string word;
while (file >> word)
{
// do whatever you want, e.g. print:
std::cout << word << std::endl;
}
file.close();
return 0;
}
word variable will contain every single word from a text file (words should be separated by space in your file).

OpenKey Windows 7 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
what have of wrong here ? Work in my notebook, but not in my PC . . .
The two are 64-bits, Windows 7 ultimate.
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
HKEY CH;
char File_Name[] = "C:\\Users\\RMS\\Desktop\\M.txt";
if(RegCreateKey(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Windows\\CurrentVersion\\Run",&CH) != 0){
printf("Erro - RegCreateKey\n");
system("PAUSE");
return -1;
}
if(RegOpenKey(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Windows\\CurrentVersion\\Run",&CH) != 0) // Abre a CH "Minha CH"
{
printf("Erro - RegOpenKey\n");
system("PAUSE");
return -1;
}
if(RegSetValueEx(CH,L"My_Value",0,REG_SZ,(LPBYTE) L"C:\\Users\\RMS\\Desktop\\M.txt",40) != 0)
printf("Erro - RegSetValue\n");
RegCloseKey(CH);
printf("\nsucesso !\n");
system("PAUSE");
return 0;
}
I found. . . Was only do this:
if(RegSetValueEx(CH,L"My_Value",0,REG_SZ,(LPBYTE) L"C:\\Users\\RMS\\Desktop\\M.txt",60) != 0)
VERY THANK GUYS !!
Your problem is that the HKLM registry key is only writable by elevated programs, and your program is not running elevated. The reason it works on one machine and not the other is that one has user access control turned down/off while the other doesn't.
If you ran the program from an elevated command prompt it will work.
Additionally, you're using L"" for the strings, but using a RegSetValueEx call with 40, which is 40 bytes, and will actually cut off the M.txt on the text you're setting (if it works at all). Where you initialize the .txt file you should use:
TCHAR File_Name[] = L"C:\\Users\\RMS\\Desktop\\M.txt";
Then for the RegSetValueEx you do:
RegSetValueEx(CH,L"My_Value",0,REG_SZ,(LPBYTE) File_Name, sizeof File_Name + sizeof(TCHAR))
This makes it the number of bytes that corresponds to the filename, plus the final NULL TCHAR.
what error do you get?
try
RegOpenKeyEx
instead of RegOpenKey, since that's for 16 bit windows.