Re: Segmentation fault and also bad initalization
Quote:
Originally Posted by
jedipenguin
My first printf ouputs: This is a message.
My second printf ouputs nothing.
You have several printf() statements.
The easiest way to make things work is to write a very simple program. Right now, you have an umpteen different things going on, and each can have their own issues.
Code:
#include <string>
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
string s2;
ifstream input("your_input_file");
if ( !input )
return 0;
while (!input.eof())
{
getline(input, s2);
cout << s2;
}
}
What do you get when you run this program on your file, and any other text file? Do you get to see all of the lines of text?
Regards,
Paul McKenzie
Re: Segmentation fault and also bad initalization
I don't have one of those and wouldn't be able to get one to work in the time before midnight tomorrow night when it's due.
My thing doesn't have a debugger. There is absolutely nothing I can do about it.
Also, I tried the cout and it's fine inside the while loop and before it but the value is gone after the while loop is over. I honestly don't know and really need you to spell it out for me.
I almost got it to work, though it's running short on the second line:
It's outputting:
This is a message.
This i
but should be outputting:
This is a message.
This is a message.
Is it something wrong with my write()?
Code:
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include<string>
#include<vector>
#include<iostream>
#include <fstream>
#include <sstream>
#define FILEPATH "./input.txt"
#define FILEPATH2 "./copy.txt"
#define BUFFER_SIZE 25
#define READ_END 0
#define WRITE_END 1
using namespace std;
int main(int argc, const char* argv[])
{
ifstream input(argv[2]);
if (!input.is_open())
{
printf("File is busted!");
return 1;
}
string s2;
vector<char> s;
if (argc !=3)
{
printf("You're forgetting some of the arguments.");
return 1;
}
char read_msg[BUFFER_SIZE];
int fd[2];
pid_t pid;
FILE *fp;
FILE *fp2;
// fp = fopen(argv[2], "r");
/* if(!fp){
fprintf(stderr, "File - %s - could not be opened.\n", FILEPATH);
return -1;
}
*/
string boohoo;
getline(input, s2);
boohoo = s2 + "\n";
cout << s2;
while (!input.eof())
{
getline(input, s2);
cout << s2;
boohoo =boohoo + s2 + "\n";
}
input.close();
cout << "Hey" << boohoo;
// s = fgets(s, strlen(s), fp); //ERROR HERE!!!
/* create the pipe */
if (pipe(fd) == -1){
fprintf(stderr, "Pipe failed");
return 1;
}
printf(boohoo.c_str());
const char* write_msg = boohoo.c_str(); //How do I get this value of s into the right format?
printf(write_msg);
printf("\n");
/* fork a child process */
pid = fork();
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
return 1;
}
string pipeMessage;
const char* pm2;
if (pid > 0) { /* parent process */
/* close the unused end of the pipe */
close(fd[READ_END]);
printf(write_msg);
/* write to the pipe */
write(fd[WRITE_END], write_msg, strlen(write_msg) + 1);
printf(write_msg);
/* close the write end of the pipe */
close(fd[WRITE_END]);
}
else { /* child process */
/* close the unused end of the pipe */
close(fd[WRITE_END]);
/* read from the pipe */
const int numchars = sizeof(read_msg)/sizeof(read_msg[0]);
read(fd[READ_END], read_msg, numchars);
printf("read %s", read_msg);
// printf("read %s", read_msg);
// fclose(fp);
ofstream output(argv[1]);
printf(read_msg);
output << read_msg;
output.close();
/* close the write end of the pipe */
close(fd[READ_END]);
// fclose(fp2);
}
return 0;
}
It's going wrong with my read I think.
As for the format, it was better before, but when I copy it, it loses it.
Re: Segmentation fault and also bad initalization
@jedipenguin: You said in a previous post that you are using 'emacs' for your coding, but also that you don't have a debugger. You should look again, as emacs almost certainly comes with support for the 'gdb' debugger built-in.
Or, as you say you are using Linux, try DDD if you want a GUI debugger.