C++ File input and output:cannot input - c++

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;
}

Related

Segmentation fault: unable to fix the problem

I'm new to C++, and I'm trying to write a project that interacts through command line. Right now, whenever I run my main (which is the executable), I always receive a segmentation fault error when the main program finished.
Edit comment:
I'm told by tutor to use as little as C++ features such as vectors or strings ... I'm also very new to C++, so i'm trying to utilize as many basic C functions as I can.
I'm
My main function looks like this:
int main(int argc, char** argv) {
cout << "starting mvote..." << endl;
int run_flag = 1;
char* actionBuffer = (char*)malloc(100 * sizeof(char));
char* action = (char*)malloc(16 * sizeof(char));
char* readPtr;
char exit[4] = { 'e','x','i','t' };
//parse command line argumentand get the filename
char* filename = argv[2];
cout << filename;
FILE* fp;
char line[64];
//from here, I'm opening the file and read it by lines
fp = fopen(filename, "r");
if (fp == NULL) {
cout << "file not exists";
return -1;
}
while (fgets(line, 64, fp) != NULL) {
cout << line << "\n";
}
fclose(fp);
while (run_flag == 1) {
cout << "what do you want?\n " << endl;
cin.getline(actionBuffer, 1024);
if (strcmp(actionBuffer, exit) == 0) {
cout << "bye!";
run_flag = 0;
break;
}
//if not exit, Look for the space in the input
readPtr = strchr(actionBuffer, ' ');
int size = readPtr - actionBuffer;
//extract the operation
strncpy(action, actionBuffer, size);
for (int i = 0; i < size; i++) {
cout << "operation:" << action[i];
}
// depend on the operation specified before the first empty space
run_flag = 0;
}
free(actionBuffer);
free(action);
return 0;
}
Description:
I first try to open up a csv file which lies in the same folder as main, and I read the file line by line. Then, I just implement a simple command where you can type exit and quit the program.
I allocate two memory, actionBuffer and action, which are used to hold command
Problem: a segmentation fault [core dumped] always exists when I type exit and hit enter, and then the process finished.
Research: So I learned that segmentation fault is due to accessing a memory that does not belongs to me. But where in my program am I trying to access such a memory?
Any advice is appreciated! Thank you.
Just to give you an idea, this would be an example of C++ code
#include<iostream>
#include<fstream>
#include<string_view>
#include<string>
#include<sstream>
#include<exception>
int main(int argc, char** argv) {
std::cout << "starting mvote...\n";
//parse command line argumentand get the filename
std::string filename = argv[2]; // NO CHECKS!
std::cout << filename <<'\n';
//from here, I'm opening the file and read it by lines
{
std::ifstream ifs(filename);
if (!ifs) {
throw std::invalid_argument("file not exists");
}
std::string line;
while (std::getline(ifs, line)) {
std::cout << line << '\n';
}
}
bool run_flag = true;
while (run_flag) {
std::cout << "what do you want?\n";
std::string userInput;
std::getline(std::cin, userInput);
if (userInput == "exit") {
std::cout << "bye!\n";
return 0;
}
std::stringstream userInputSs(userInput);
std::string operation;
while(userInputSs >> operation){
std::cout << "operation: " << operation << '\n';
}
}
}

ifstream: /dev/stdin is not working the same as std::cin

For my formation, an exercise ask us to create a program similar to the linux 'cat' command.
So to read the file, i use an ifstream, and everything work fine for regular file.
But not when i try to open /dev/ files like /dev/stdin: the 'enter' is not detected and so, getline really exit only when the fd is being closed (with a CTRL-D).
The problem seems to be around how ifstream or getline handle reading, because with the regular 'read' function from libc, this problem is not to be seen.
Here is my code:
#include <iostream>
#include <string>
#include <fstream>
#include <errno.h>
#ifndef PROGRAM_NAME
# define PROGRAM_NAME "cato9tails"
#endif
int g_exitCode = 0;
void
displayErrno(std::string &file)
{
if (errno)
{
g_exitCode = 1;
std::cerr << PROGRAM_NAME << ": " << file << ": " << strerror(errno) << std::endl;
}
}
void
handleStream(std::string file, std::istream &stream)
{
std::string read;
stream.peek(); /* try to read: will set fail bit if it is a folder. */
if (!stream.good())
displayErrno(file);
while (stream.good())
{
std::getline(stream, read);
std::cout << read;
if (stream.eof())
break;
std::cout << std::endl;
}
}
int
main(int argc, char **argv)
{
if (argc == 1)
handleStream("", std::cin);
else
{
for (int index = 1; index < argc; index++)
{
errno = 0;
std::string file = std::string(argv[index]);
std::ifstream stream(file, std::ifstream::in);
if (stream.is_open())
{
handleStream(file, stream);
stream.close();
}
else
displayErrno(file);
}
}
return (g_exitCode);
}
We can only use method from libcpp.
I have search this problem for a long time, and i only find this post where they seems to have a very similar problem to me:
https://github.com/bigartm/bigartm/pull/258#issuecomment-128131871
But found no really usable solution from them.
I tried to do a very ugly solution but... well...:
bool
isUnixStdFile(std::string file)
{
return (file == "/dev/stdin" || file == "/dev/stdout" || file == "/dev/stderr"
|| file == "/dev/fd/0" || file == "/dev/fd/1" || file == "/dev/fd/2");
}
...
if (isUnixStdFile(file))
handleStream(file, std::cin);
else
{
std::ifstream stream(file, std::ifstream::in);
...
As you can see, a lot of files are missing, this can only be called a temporary solution.
Any help would be appreciated!
The following code worked for me to deal with /dev/fd files or when using shell substitute syntax:
std::ifstream stream(file_name);
std::cout << "Opening file '" << file_name << "'" << std::endl;
if (stream.fail() || !stream.good())
{
std::cout << "Error: Failed to open file '" << file_name << "'" << std::endl;
return false;
}
while (!stream.eof() && stream.good() && stream.peek() != EOF)
{
std::getline(stream, buffer);
std::cout << buffer << std::endl;
}
stream.close();
Basically std::getline() fails when content from the special file is not ready yet.

std::wcout will not print after certain amounts of characters

I've been beating my head against a wall with this one for a while. I'm only trying to make a simple application to read out the contents of a file. Here's some of the code:
errno_t error;
if ((error = fopen_s(&f, file, "r")) == 0) {
while (true) {
std::wcout << std::endl << "NEW RUN" << std::endl;
wchar_t content[4096];
if (fgetswc(content, 4096, f) == 4096) {
std::wcout << content;
std::wcout.flush();
}
else {
std::wcout << content;
std::wcout.flush();
break;
}
}
fclose(f);
std::wcout << "PLEASE PRINT THIS NOW";
system("pause");
return 0;
}
And the custom fgetswc function:
int fgetswc(wchar_t buffer[], int count, FILE * f) {
for (int i = 0; i < count; i = i + 1) {
wchar_t c = fgetwc(f);
if (c != WEOF) {
buffer[i] = c;
} else {
return i;
}
}
return count;
}
It reads the first 4096 bytes out of the file, but then subsequent std::wcout calls will not print out to the console I have. It reads the rest of the file and ends successfully, as I can see using breakpoints and the debugger. content gets filled up every iteration. I also attempted putting in debug statements, but even those don't get printed. Am I just doing something wrong? As far as I can tell there's no special characters in my file, it's just a log file.
std::wcout << content;
This is effectively calling std::wostream::operator<<(const wchar_t *). It doesn't know that content is not a ␀-terminated string. In fact, it can't possibly know that it has valid length 4096 in the first case and some amount less in the second case (you don't save the return value of fgetswc).

How to log all input (cin) into file in C++

Actually, I'm working on a minishell. My functions work, but when I want to log the whole cin stuff (commands, parameters, output) into a file, nothing appears in the file. Nowhere can I find something to handle with full input and output with parameters.
I hope someone can help me.
My code:
using namespace std;
ofstream outputFile;
void read_command(char *com, char **par){
fprintf(stdout, "$");
cin >> com;
outputFile.open("logging.txt"); // file opened but nothing APPEARS IN IT????
if(strcmp(com,"date")== 0){ // DATE
time_t rawtime;
time ( &rawtime );
printf ( "%s", ctime (&rawtime) );
}
else if(strcmp(com,"echo")== 0) // ECHO
{
string echo_part;
cin >> echo_part;
cout << echo_part << endl;
}
else if(strcmp(com,"sleep")== 0){ // SLEEP
int howlong = 0;
cin >> howlong;
cout << "seconds: " << howlong << "....zZZzzZzz" << endl;
sleep(howlong);
}
else if(strcmp(com,"ps")== 0) // PROCESS
{
execlp("/bin/ps","ps","-A",NULL); // ps - command
}
}
void handler(int p) { // CTR-C handler
cout << endl;
cout << "Bye !" << endl;
outputFile.close();
alarm(1); // 2 seconds alarm ends process with kill
}
int main(){
int childPid;
int status;
char command[20];
char *parameters[60];
signal(SIGINT,&handler); // CTR-C exit disabled
while (1) {
read_command(command, parameters);
if ((childPid = fork()) == -1) {
fprintf(stderr,"can't fork\n");
exit(1);
}
else if (childPid == 0) { //child
execv(command, parameters);
exit(0);
}
else { // parent process
wait(&status);
}
}
}
You re-open the output stream outputFile for every line, overwriting the file with each new command.
Edit: As the other posters noted, not actually writing something to outputFile might be a second reason.
You open outputFile, but never write anything to it. What should appear there?
To output something to the file, try
outputFile << something
there are no
outputFile << ...;
so you are not writing to the file
Your code contains a lot of potential memory access violations.
There are libraries to help you in what you are trying to do (reading and interpreting user input), for instance the GNU Readline library, which is coded in C (but can be used by C++ code, as is the case for all the C-written libraries).
There are some nice C++ wrappers, such as for instance SReadlineWrapper.

writing to a file using stream in C++

I want to some text to output to a file. I heard that it is better to stream the data rather than creating a large string and outputing that. Presently I am creating a large string and outputing to a file. Request to provide an sample code on how to stream a data and write to a file using C++.
Thanks!
#include <fstream>
int main()
{
std::ofstream fout("filename.txt");
fout << "Hello";
fout << 5;
fout << std::endl;
fout << "end";
}
Your file now contains this:
Hello5
end
See more info on std::ofstream for details.
HTH
File writing already uses buffering. If it is not efficient for you, you can actually modify the filebuf, eg increase its size or use a custom one.
Avoid doing unnecessary flushes of your buffer, which is done with endl. That is the most "abused" feature of file-writing.
The simplest way to create a file-stream for outputting is:
#include <fstream>
int main( int argc, char * argv[])
{
if( argc > 1 )
{
std::ofstream outputFile( argv[1] );
if( outputFile )
{
outputFile << 99 << '\t' << 158 << '\n'; // write some delimited numbers
std::vector< unsigned char > buf;
// write some data into buf
outputFile.write( &buf[0], buf.size() ); // write binary to the output stream
}
else
{
std::cerr << "Failure opening " << argv[1] << '\n';
return -1;
}
}
else
{
std::cerr << "Usage " << argv[0] << " <output file>\n";
return -2;
}
return 0;
}