I have opened a file using CreateFile fn and tried to print data into the file. Since the data includes some print statements like
wprintf(L"Channel %s was not found.\n", pwsPath);
The declaration for DATA and pwsPath
#include <iostream>
#include <sstream>
using namespace std;
string data;
LPWSTR pwsPath = L"Channel1";
I tried to use stringstream to get the data and convert it to a LPCVOID to use the WriteFile fn as shown
hFile1 = CreateFile(L"MyFile.txt", // name of the write
GENERIC_WRITE, // open for writing
0, // do not share
NULL, // default security
CREATE_ALWAYS, // create new file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL);
std::stringstream ss;
ss << "Channel" << pwsPath << "was not found.";
ss >> data;
cout << data; // data contains value only till the first space i.e Channel093902
cin>>data;
bErrorFlag = WriteFile(
hFile1, // open file handle
data.c_str(), // start of data to write
dwBytesToWrite, // number of bytes to write
&dwBytesWritten, // number of bytes that were written
NULL);
Is it possible for variable data to include spaces from the stringstream ??
OR
Is there any other way other than stringstream to get the data from such print statements and write to the file as such?
The >> operator will deliver the next 'word' in the stream into the string object you have given it. It breaks at the first white space as you have found. There are a couple of ways to achieve what you want. The most conformant is to open the output file as an ofstream:
#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;
int main()
{
std::string pwsPath { "[enter path here]" };
std::stringstream ss;
ss << "Channel " << pwsPath << " was not found.";
std::ofstream outFile("myFile.txt");
outFile << ss.rdbuf();
outFile.close();
std::ifstream inFile("myFile.txt");
cout << inFile.rdbuf();
return 0;
}
otherwise you can get the internal string from the ostringstream:
std::string myData = ss.str();
size_t dataLength = myData.length();
DWORD dwBytesWritten = 0;
BOOL bErrorFlag = WriteFile(
hFile1, // open file handle
myData.data(), // start of data to write
DWORD(dataLength), // number of bytes to write
&dwBytesWritten, // number of bytes that were written
NULL);
Unless you have a good reason to use CreateFile and WriteFile, consider using std objects all the way.
Your code could look like this:
#include <iostream>
#include <fstream> // add this
#include <sstream> // remove this unless used elsewhere
// your pwsPath
std::wstring path{ L"Channel1" };
std::wofstream out{ L"MyFile.txt", std::wofstream::trunc };
// skip the stringstream completely
out << "Channel " << path << " was not found."
Related
In my program I use an external function which generates output to me and I don't want it not to be generated by that function alone,
it's possible?
int main()
{
int a;
//I don't want the output of this function
a = function();
//now i want output
cout << "the result is : " << a;
}
Is it possible?
EDIT:
The function is in an external library.
Using only standard C++ where no dup-like functions exist, you could open a temporary std::FILE and std::swap with stdout.
#include <cerrno>
#include <cstring>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
#include <sstream>
// extern "C" int function(); // an assumption
// A helper class to temporarilly redirect the output to stdout to a file and to read
// the content of the file afterwards.
class BufferStdout {
public:
// the collector string is used for collecting the output to stdout
BufferStdout (std::string& collector) :
m_collector(collector),
fp(std::fopen("output.txt", "w"))
{
if(fp == nullptr) throw std::runtime_error(std::strerror(errno));
std::swap(stdout, fp); // swap stdout and the temp file
}
~BufferStdout () {
std::swap(stdout, fp); // swap back
std::fclose(fp);
// read the content of the temp file into m_collector
if(std::ifstream is("output.txt"); is) {
m_collector.append(std::istreambuf_iterator<char>(is),
std::istreambuf_iterator<char>{});
}
std::remove("output.txt"); // cleanup
}
private:
std::string& m_collector;
std::FILE* fp;
};
int main() {
std::string collector; // the string that will contain the output from function()
int a;
{
BufferStdout foo(collector);
a = function();
}
std::cout << "the result is : " << a << '\n';
std::cout << "Collected from function():\n";
std::cout << collector << '\n';
}
Yes it is generally possible but a bit complicated, a similar question is in Suppress output to cout from linked library
In addition to you can redirect stdout before invoking the shared library function and then redirect it again after the use of the shared library function in the however this is also a suboptimal solution. Best solution would be to adapt the shared library
// Cpp program to redirect cout to a file
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
fstream file;
file.open("cout.txt", ios::out);
string line;
// Backup streambuffers of cout
streambuf* stream_buffer_cout = cout.rdbuf();
streambuf* stream_buffer_cin = cin.rdbuf();
// Get the streambuffer of the file
streambuf* stream_buffer_file = file.rdbuf();
// Redirect cout to file
cout.rdbuf(stream_buffer_file);
cout << "This line written to file" << endl;
// Redirect cout back to screen
cout.rdbuf(stream_buffer_cout);
cout << "This line is written to screen" << endl;
file.close();
return 0;
}
Note: The above steps can be condensed into a single step
auto cout_buf = cout.rdbuf(file.rdbuf())
// sets couts streambuffer and returns the old
streambuffer back to cout_buf
source : https://www.geeksforgeeks.org/io-redirection-c/
use
fclose(stdout);
with this function you will suppress any printf inside your libraries.
Of course you can not print other stuff inside your software.
Hi my goal is to take each line from input.txt load them into a vector and then copy each vector to the clipboard every 1 second.
So far I am able to load the file into a vector using getFileContent(std::string fileName, std::vector<std::string> & vecOfStrs)
I am also able to copy a string to the clipboard if I replace:
cout << "Lines Copying " << endl;
with
cout << "Please enter sentence: "; cin >> AAA;
Using the users input...
However, when I try to load the vector called line I get 0 char(s) copied to clipboard?
What am I doing wrong? Any pointers or suggestions will be greatly appreciated..
Using:
Windows 10 64x
Microsoft Visual Studio Community 2015
Version 14.0.23107.0 D14REL
Microsoft .NET Framework
Version 4.7.02046
Visual C++ 2015 00322-20000-00000-AA355
Microsoft Visual C++ 2015
input.txt:
The
Brown
Fox
Jumps
Script:
// copyfilelines.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>
#include <vector>
#include <direct.h>
#include <conio.h>
#include <stdio.h>
#include <cstdlib>
#include <winuser.h>
#include <cmath>
#include <iomanip>
#include <complex>
void toClipboard(HWND hwnd, const std::string &s);
/*
* It will iterate through all the lines in file and
* put them in given vector then copy vector to clipboard.
*/
//1. Open file and put each line into a vector.
bool getFileContent(std::string fileName, std::vector<std::string> & vecOfStrs)
{
// Open the File
std::ifstream in(fileName.c_str());
// Check if object is valid.
if (!in)
{
std::cerr << "Cannot open the File : " << fileName << std::endl;
return false;
}
std::string str;
// Read the next line from File untill it reaches the end.
while (std::getline(in, str))
{
// Line contains string of length > 0 then save it in vector.
if (str.size() > 0)
vecOfStrs.push_back(str);
}
// Close The File.
in.close();
return true;
}
//2. Declare clipboard functions at file scope.
void toClipboard(HWND hwnd, const std::string &s) {
OpenClipboard(hwnd);
EmptyClipboard();
HGLOBAL hg = GlobalAlloc(GMEM_MOVEABLE, s.size() + 1);
if (!hg) {
CloseClipboard();
return;
}
memcpy(GlobalLock(hg), s.c_str(), s.size() + 1);
GlobalUnlock(hg);
SetClipboardData(CF_TEXT, hg);
CloseClipboard();
GlobalFree(hg);
}
int main()
{
std::vector<std::string> vecOfStr;
// Get the contents of file in a vector.
bool result = getFileContent("input.txt", vecOfStr);
if (result)
{
// Print the vector contents.
for (std::string & line : vecOfStr)
std::cout << line << std::endl;
// Copy vector to clipboard.
using namespace std;
string line;
cout << endl;
cout << endl;
cout << "Lines Copying " << endl;
cout << endl;
cout << endl;
cout << "Lines Copied To The Clipboard: ";
cout << endl;
cout << line << endl;
// 1. Strlen takes a const char*, so have to call the strings c_str() method
// (but it would be better to use len = line.length() instead)
size_t len = strlen(line.c_str());
cout << len << " char(s)" << endl;
// Get desktop windows and the call toClipboard.
HWND hwnd = GetDesktopWindow();
toClipboard(hwnd, line);
//User input processing.
//cin.clear();
//cin.ignore(255, '\n');
//cin.get();
}
return 0;
}
EDIT:
Updated script
I added a std::ostream_iterator to allow streaming of a vector:
std::stringstream ss;
// Populate
std::copy(vecOfStr.begin(), vecOfStr.end(), std::ostream_iterator<std::string>(ss, "\n"));
// Display
std::cout << ss.str() << std::endl;
// Copy vector to clipboard.
size_t len = strlen(ss.str().c_str());
Full Updated Script:
Streams content of input.txt and copies all streamed content to clipboard.
// copyfilelines.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <fstream>
#include <string>
#include <vector>
#include <direct.h>
#include <conio.h>
#include <stdio.h>
#include <cstdlib>
#include <winuser.h>
#include <cmath>
#include <iomanip>
#include <complex>
#include <iostream>
#include <sstream>
#include <iterator>
void toClipboard(HWND hwnd, const std::string &s);
/*
* It will iterate through all the lines in file and
* put them in given vector then copy vector to clipboard.
*/
//1. Open file and put each line into a vector.
bool getFileContent(std::string fileName, std::vector<std::string> & vecOfStrs)
{
// Open the File
std::ifstream in(fileName.c_str());
// Check if object is valid.
if (!in)
{
std::cerr << "Cannot open the File : " << fileName << std::endl;
return false;
}
std::string str;
// Read the next line from File untill it reaches the end.
while (std::getline(in, str))
{
// Line contains string of length > 0 then save it in vector.
if (str.size() > 0)
vecOfStrs.push_back(str);
}
// Close The File.
in.close();
return true;
}
//2. Declare clipboard functions at file scope.
void toClipboard(HWND hwnd, const std::string &s) {
OpenClipboard(hwnd);
EmptyClipboard();
HGLOBAL hg = GlobalAlloc(GMEM_MOVEABLE, s.size() + 1);
if (!hg) {
CloseClipboard();
return;
}
memcpy(GlobalLock(hg), s.c_str(), s.size() + 1);
GlobalUnlock(hg);
SetClipboardData(CF_TEXT, hg);
CloseClipboard();
GlobalFree(hg);
}
int main()
{
std::vector<std::string> vecOfStr;
// Get the contents of file in a vector.
bool result = getFileContent("input.txt", vecOfStr);
if (result)
{
std::stringstream ss;
// Populate
std::copy(vecOfStr.begin(), vecOfStr.end(), std::ostream_iterator<std::string>(ss, "\n"));
// Display
std::cout << ss.str() << std::endl;
// Copy vector to clipboard.
size_t len = strlen(ss.str().c_str());
// Get desktop windows and the call toClipboard.
HWND hwnd = GetDesktopWindow();
toClipboard(hwnd, ss.str());
Sleep(100000);
}
return 0;
}
Ignoring all the irrelevant printing to cout, your code is:
if (result)
{
// Copy vector to clipboard.
using namespace std;
string line;
size_t len = strlen(line.c_str());
// Get desktop windows and the call toClipboard.
HWND hwnd = GetDesktopWindow();
toClipboard(hwnd, line);
}
You have a default constructed string called line, and you copy that to the clipboard. Not surprisingly, it contains no characters. I think you need to create a stringstream and print the vector to that, and then copy that to the clipboard.
I'm trying to read in an .exe and write it back out. My code works with .txt files but for some reason it is breaking executables. What am I doing wrong?
I'm not sure if I am reading it wrong or writing it wrong..
#include <string>
#include <vector>
#include <iostream>
#include <filesystem>
#include <unordered_set>
#include <Windows.h>
unsigned char *ReadFileAsBytes(std::string filepath, DWORD &buffer_len)
{
std::ifstream ifs(filepath, std::ofstream::binary | std::ifstream::ate);
if (!ifs.is_open())
{
return(nullptr);
}
// Go To End
ifs.seekg(0, ifs.end);
// Get Position (Size)
buffer_len = static_cast<DWORD>(ifs.tellg());
// Go To Beginning
ifs.seekg(0, ifs.beg);
// Allocate New Char Buffer The Size Of File
PBYTE buffer = new BYTE[buffer_len];
ifs.read(reinterpret_cast<char*>(buffer), buffer_len);
ifs.close();
return buffer;
}
void WriteToFile(std::string argLocation, unsigned char *argContents, int argSize)
{
std::ofstream myfile;
myfile.open(argLocation);
myfile.write((const char *)argContents, argSize);
myfile.close();
}
int main()
{
// Config
static std::string szLocation = "C:\\Users\\Admin\\Desktop\\putty.exe";
static std::string szOutLoc = "C:\\Users\\Admin\\Desktop\\putty2.exe";
DWORD dwLen;
unsigned char *szBytesIn = ReadFileAsBytes(szLocation, dwLen);
std::cout << "Read In " << dwLen << " Bytes" << std::endl;
// Write To File
WriteToFile(szOutLoc, szBytesIn, dwLen);
system("pause");
}
You open input file in binary mode, but in this code
std::ofstream myfile;
myfile.open(argLocation);
you open output file without binary mode. And there is no reason to call open separately:
std::ofstream myfile( argLocation, std::ios::out | std::ios::binary | std::ios::trunc);
I tried many solutions, nothing works, absolute and relative paths. I changed directories as well, etc. My code always worked this way, and I got no clue what can be wrong.
Two examples of what I did:
// Read a file into memory
#include <iostream> // std::cout
#include <fstream> // std::ifstream
int main () {
std::ifstream is ("test.txt", std::ifstream::binary);
if (is) {
// Get length of file:
is.seekg (0, is.end);
int length = is.tellg();
is.seekg (0, is.beg);
char * buffer = new char [length];
std::cout << "Reading " << length << " characters... ";
// Read data as a block:
is.read (buffer,length);
if (is)
std::cout << "all characters read successfully.";
else
std::cout << "error: only " << is.gcount() << " could be read";
is.close();
// ...buffer contains the entire file...
std::cout << buffer;
system("PAUSE");
delete[] buffer;
}
return 0;
}
and
char pfad[256]; //The path to the application is stored here.
_getcwd( pfad, 256);
std::string truepfad;
truepfad = pfad;
truepfad.append("\\test.txt");
fstream f("C:\\Users\\Etix\\Documents\\test.txt");
string s;
if (!f)
std::cout << truepfad;
else
{
std::cout << "open file";
while (getline(f, s)){
std::cout << s;
}
}
The errors I get while using your code are:
Error: Expected a declaration.
Error: This declaration has no storage class or type specifier.
I think the problem might be that your missing some #include's. I saw you need #include
<string> and several others as well as an improperly made if statement at line 44
(with the #include <string>).
The following C++ code is to read a txt file and then write the numbers of chars in each line and the number of all chars in the txt file.I use MS Visual Studio 2008.But something is wrong.Only the number of all chars is input into the txt files,but the numbers of each line are not input into the txt files.Now I cannot figure it out.Could someone give me some advice?Thanks a lot!
And my another question is what should I do to insert something in the middle of the txt file?
This is my Code:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream inOut("copy.txt",ios::in | ios::out | ios::app);
if (!inOut)
{
cerr << "ERROR:cannot open file!" << endl;
return -1;
}
int cnt = 0;
char ch;
inOut.seekg(0);
while(inOut.get(ch))
{
cout.put(ch);
++cnt;
if ('\n' == ch)
{
ios::pos_type mark = inOut.tellg();
if (!inOut)
{
cerr << "ERROR!" << endl;
return -1;
}
inOut << cnt;
inOut.put(' ');
inOut.seekg(mark);
}
}
inOut.clear();
inOut << cnt << endl;
cout << "[" << cnt << "]" << endl;
}
The txt file before running:
The txt file after running:
The result in command line:
I don't know fstream's very well, but I think you want to change the way you are doing this. You can think of a file as a contiguous piece of memory. Appending on the end is easy, but inserting in the middle can be problematic. In particular, if you do insert something, then your seekg might not be valid.
I would recommend three strategies:
Understand what is going on currently (try closing file before seekg and see if anything gets written by the inOut << cnt;)
Read from one file, write to a different file -- this will most likely be more efficient and less complicated than trying to modify the file in place.
Read from source file, store and modify in memory, write out modified buffer to original file. For large files, this might be less efficient than #2, but it means you don't need two files on disk.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream inOut("copy.txt",ios::in | ios::out | ios::app);
if (!inOut)
{
cerr << "ERROR:cannot open file!" << endl;
return -1;
}
int cnt = 0;
int cntline=0;
char ch;
inOut.seekg(0);
while(inOut.get(ch))
{
cout.put(ch);
//++cnt it include '\n' it should put behind of "if"
if ('\n' == ch)
{ ios::pos_type mark = inOut.tellg();
if (!inOut)
{
cerr << "ERROR!" << endl;
return -1;
}
cntline=cnt-cntline; //add it to account the number of chars of each line
inOut.seekg(0,ios::end); //here!!!!!!!!!!!!!!!!!!!!!!!!!!! add this , the resault is finally right! the reason it's down here.
inOut << cntline;
cntline=cnt; // and here! it can assure the number of the chars of each line be right!
inOut.put(' ');
inOut.seekg(mark);
}
else
{
++cnt; // it's here!
}
}
inOut.clear();
inOut << cnt << endl;
cout << "[" << cnt << "]" << endl;
}
inOut.seekg(0,ios::end)
i'am not sure it,but you can take it a reference.
you open file with "ios::app",so the VS 2008 only allows you to add text start from the end of file (it won't happened in vc 6.0).
if the file is :
it's compile by
vs 2008
when get the first '\n' the file pointer is pointing to'vs 2008'(yes,it just like the common pointer point the string ). and i debug it then find a value of the stream object named _Wrotesome .its value is false!
so i think the compiler think the file pointer point at o const sting.so you just can't write anything whis this position. so i add the code inOut.seekg(0,ios::end);.now you
can write anything you want to this text!
wish this can help you !
I don't know why do you choose std::fstream as your tool. If std::fstream is not asked or necessary, I would like to provide a example to solve your problem. Here's some limitation:
a. I read all the file into memory in one ReadFile operation. If your file is large, you may replace it with a loop.
b. I suppose your line separator is '\n'.
c. I add [line_count] before the '\n', which look like better.
here's my code.
#include "windows.h"
#include "sstream"
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hFile = ::CreateFile(L"C:\\Users\\wujian\\Desktop\\pingback - Copy.log", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE )
{
DWORD dwSize = ::GetFileSize(hFile, NULL);
if (dwSize)
{
char *pBuffer = new char[dwSize];
DWORD dwRead = 0;
::ReadFile(hFile, pBuffer, dwSize, &dwRead, NULL);
if (dwRead == dwSize)
{
std::stringstream ss;
int iPos = 0;
int iLine = 0;
while (iPos < dwSize)
{
if (pBuffer[iPos] == '\n')
{
ss << '[' << iLine << ']';
iLine = 0;
}
ss << pBuffer[iPos];
iPos ++, iLine ++;
}
ss << '[' << dwSize << ']';
::SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
DWORD dwWrite = 0;
::WriteFile(hFile, ss.str().c_str(), ss.str().length(), &dwWrite, NULL;
}
::CloseHandle(hFile);
}
}
return 0;
}