0

I have the following code...

http://pastebin.com/KjzArbcg ^ Full code

//the issue - this throws a Segmentation fault
char *inname1 = "HW11F1.txt";
cin >> inname1;
ifstream infile1(inname1);

The assignment is the following.. "Write a program that merges the numbers in two files and writes all the numbers into a third file. Each input file contains a list of numbers of type int in sorted order from the smallest to the largest. After the program is run, the output file will contain all the numbers in the two input files in one longer list in sorted order from smallest to largest. Your program should define a function that is called with the two input file streams and the output file stream as three arguments."

I have it working for default filenames just fine, everything seems to work how I want it to. The issue is when I try to do cin >> inname1;

The program crashes with a Segmentation fault (core dump). Any idea what I'm doing wrong, or how to fix it? Thanks in advance.

Coty
  • 143
  • 1
  • 11
  • Use `std::string` instead of `char*`. – Kiril Kirov Nov 21 '13 at 09:04
  • 2
    Please post your code directly in the question, and if the program is fairly long, strip out any non-essential until you get a minimal test case that we can copy and copy to see the error. (For some reason pastebin is blocked for me anyway). – BoBTFish Nov 21 '13 at 09:04
  • @KirilKirov `or` may be rarely used, but it's just as standard as `||`. – Angew is no longer proud of SO Nov 21 '13 at 09:05
  • @KirilKirov Not having seen the code, I agree with the first comment, but what's wrong with the second one? It is less common, [but otherwise identical, no?](http://stackoverflow.com/a/17110423/1171191) – BoBTFish Nov 21 '13 at 09:05
  • Instead of `array1.resize(pos+1); array1[pos] = i;` you can use just `std::vector::push_bacK` - you should really learn how to read documentation and check libraries' APIs – Kiril Kirov Nov 21 '13 at 09:05
  • 1
    @KirilKirov No, it's not. It's still perfectly legal and not deprecated. – Angew is no longer proud of SO Nov 21 '13 at 09:06
  • Kiril Kirov, using std::string makes the compiler throw all sorts of errors. http://puu.sh/5oOfP.png -- BoBTFish, why? Pastebin is fine for something like this, no? I set it to never expire. – Coty Nov 21 '13 at 09:06
  • @Angew - okay, my bad, point taken. I'll remove the remark about it. Thanks for the info. – Kiril Kirov Nov 21 '13 at 09:07
  • To open a stream using a `std::string` you need to use `std::ifstream mystream (mystring.c_str())` if you do not have `C++11` support. (In 2011 they fixed it to accept a `std::string`, but before that it needed a `const char *`, which you can get with `c_str()`). – BoBTFish Nov 21 '13 at 09:07
  • 1
    @user2993567 No, it's not fine. As per Stack Overflow policy, all questions and answers should be self-contained. External links can be used for non-essential supplementary material, but each post should still make sense without them. – Angew is no longer proud of SO Nov 21 '13 at 09:07
  • @Angew Alright, I edited it – Coty Nov 21 '13 at 09:10
  • @KirilKirov Okay so I edited it to show how you said, and now I get this - http://puu.sh/5oOwd.png - Here is a new pastebin so you can see what I'm doing - http://pastebin.com/TkD1bnP0 – Coty Nov 21 '13 at 09:17
  • @user2993567 - see http://stackoverflow.com/questions/6320995/why-i-can-not-cout-a-string/6321005#6321005 - it's the same for `cin` - try to `#include ` – Kiril Kirov Nov 21 '13 at 09:19
  • @KirilKirov Alright, I included . Now I'm getting this http://puu.sh/5oOEi.png - reading through it tells me I'm doing something incorrect with std::ofstream outfile (inname3.c_str()); outfile.open(inname3); - would you mind telling me what I'm doing wrong now? Edit: nevermind - I'm stupid. I forgot another c_str(); – Coty Nov 21 '13 at 09:23
  • @user2993567 - read the errors more carefully, see my second comment (about the documentation and APIs) and also read the other answers..... Show some effort. – Kiril Kirov Nov 21 '13 at 09:27
  • @KirilKirov Sorry, edited my post. I don't mean to come off as lazy. (It's 3AM here. It's for a class, and I'm pretty wore out tbh) – Coty Nov 21 '13 at 09:33

3 Answers3

1
char *inname1 = "HW11F1.txt";

This makes inname1 point to the read-only character literal "HW11F1.txt".

cin >> inname1;

This tries to read into that literal, which of course fails (the literal is probably stored in read-only memory).

Seeing as you're using C++ and not C, you should avoid C-style strings as much as you can and use std::string instead.

Before C++11, file stream constructors only accepted C-style strings, so you will have to modify the stream construction like this:

ifstream infile1(inname1.c_str());
Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
0

The problem is that inname1 is not properly allocated. If you used std::string instead, (as suggested by Kiril Kirov), it would allocate the necessary amount of bytes to store the input from cin before copying.

Domi
  • 22,151
  • 15
  • 92
  • 122
  • http://puu.sh/5oOfP.png - I just changed char *inname3 to std::string inname3 (for all three) and it throws this error. – Coty Nov 21 '13 at 09:08
  • @user2993567 See my latest comment on the question. Use `c_str()`. – BoBTFish Nov 21 '13 at 09:09
0

Okay so, this is how I fixed it.

#include <string> 
#include <iostream>

std::string inname1 = "HW11F1.txt";
std::string inname2 = "HW11F2.txt";
std::string inname3 = "output.txt";
cin >> inname1; 

//blah blah blah

std::ifstream infile1 (inname1.c_str());

//blah blah blah

std::ofstream outfile (inname3.c_str());
outfile.open(inname3.c_str());

Here's a pastebin of the final thing (well, final as in the issue at hand is fixed)...

http://pastebin.com/V6VErNDC

Thank you to everyone that helped. Stackoverflow always seems to save my butt.

Coty
  • 143
  • 1
  • 11