C++ shared_ptr and reading from the class - c++

I have a problem with simple reading from the file that shares file pointer between a few objects (It works for me with just simple istream, but not when I am using shared pointer of istream pointers).
I am trying to read the whole file to the buffer (file itself is a few lines long.
The code compiles, but throws segmentation fault.
The class that uses shared_ptr:
RecordsSplitter::RecordsSplitter(char *filename):iStream( new ifstream(filename, ifstream::in|ifstream::binary))
{
}
string RecordsSplitter::buildRecord() {
char *buffer;
int buffer_length;
iStream->seekg (0, ios::end)_
buffer_length = iStream->tellg();
cout << buffer_length;
iStream->seekg(0, ios::beg);
iStream->read(buffer,buffer_length);
iStream->close();
cout << buffer;
return 0;
}
int main(int argc, char* argv[]) {
RecordsSplitter *splitter;
splitter = new RecordsSplitter(argv[2]);
int return_num = splitter->buildRecord();
return 0;
}

You declare your buffer but you don't initialise it anywhere.
You need this in your buildRecord function or a use of malloc if you so desire.
buffer = new char[buffer_length];
Your seg fault is caused by this uninitialised pointer
Don't forget to clean up!
delete[] buffer;

Related

c++ char array to char pointer in struct

I have a struct containing a char pointer and I have a char array containing data. I would like to have the data within the char array copied to the char pointer.
I have tried strcpy and strncpy but I get a seg fault on both, when trying to copy the data over. I cant figure out what I am doing wrong.
struct
struct Message
{
Header hdr;
char *dataArr;
Footer ftr;
};
main
int main(){
// create message struct
Message send1;
// create stream
std::ostringstream outStream;
//
// STREAM GETS DATA HERE
//
std::string temp = outStream.str();
char arr[temp.size()];
strcpy(arr, temp.c_str());
int sz = sizeof(arr)/sizeof(char);
// print arr size and contents
std::cout << "arr size: " << sz << "\n";
for(int i=0; i<sz; i++){
std::cout << arr[i];
}
// copy char array into structs char pointer
//strncpy(send1.dataArr, arr, sz);
strcpy(send1.dataArr, arr); <-- Segmentation fault here
return 0;
}
You first need to allocate memory to copy your data.
Or use strdup that will do it for you

sprintf buffer issue, wrong assignment to char array

I got an issue with sprintf buffer.
As you can see in the code down below I'm saving with sprintf a char array to the buffer, so pFile can check if there's a file named like that in the folder. If it's found, the buffer value will be assigned to timecycles[numCycles], and numCycles will be increased. Example: timecycles[0] = "timecyc1.dat". It works well, and as you can see in the console output it recognizes that there are only timecyc1.dat and timecyc5.dat in the folder. But as long as I want to read timecycles with a for loop, both indexes have the value "timecyc9.dat", eventhough it should be "timecyc1.dat" for timecycles[0] and "timecyc5.dat" for timecycles1. Second thing is, how can I write the code so readTimecycles() returns char* timecycles, and I could just initialize it in the main function with char* timecycles[9] = readTimecycles() or anything like that?
Console output
#include <iostream>
#include <cstdio>
char* timecycles[9];
void readTimecycles()
{
char buffer[256];
int numCycles = 0;
FILE* pFile = NULL;
for (int i = 1; i < 10; i++)
{
sprintf(buffer, "timecyc%d.dat", i);
pFile = fopen(buffer, "r");
if (pFile != NULL)
{
timecycles[numCycles] = buffer;
numCycles++;
std::cout << buffer << std::endl; //to see if the buffer is correct
}
}
for (int i = 0; i < numCycles; i++)
{
std::cout << timecycles[i] << std::endl; //here's the issue with timecyc9.dat
}
}
int main()
{
readTimecycles();
return 0;
}
With the assignment
timecycles[numCycles] = buffer;
you make all pointers point to the same buffer, since you only have a single buffer.
Since you're programming in C++ you could easily solve your problem by using std::string instead.
If I would remake your code into something a little-more C++-ish and less C-ish, it could look something like
std::array<std::string, 9> readTimeCycles()
{
std::array<std::string, 9> timecycles;
for (size_t i = 0; i < timecycles.size(); ++i)
{
// Format the file-name
std::string filename = "timecyc" + std::to_string(i + 1) + ".dat";
std::ifstream file(filename);
if (file)
{
// File was opened okay
timecycles[i] = filename;
}
}
return timecycles;
}
References:
std::array
std::string
std::to_string
std::ifstream
The fundamental problem is that your notion of a string doesn't match what a 'char array' is in C++. In particular you think that because you assign timecycles[numCycles] = buffer; somehow the chars of the char array are copied. But in C++ all that is being copied is a pointer, so timecycles ends up with multiple pointers to the same buffer. And that's not to mention the problem you will have that when you exit the readTimecycles function. At that point you will have multiple pointers to a buffer which no longer exists as it gets destroyed when you exit the readTimecycles function.
The way to fix this is to use C++ code that does match your expectations. In particular a std::string will copy in the way you expect it to. Here's how you can change your code to use std::string
#include <string>
std::string timecycles[9];
timecycles[numCycles] = buffer; // now this really does copy a string

Trying to reverse the contents of the file and reverse the file's name into a new file?

So here's my code, but I don't know how to make the new file's name the original file's name reversed.
#include
int main(int argc, char *argv[]){
string text;
ifstream orig;
reverse(name[0],name[name.size()]);
orig.open(argv[1], ios::binary);
char *c = new char[1];
if(orig) {
orig.seekg(0, orig.end);
int length = orig.tellg();
orig.seekg(0, orig.beg);
}
while(orig.read(c,1)) text += c;
std::reverse(text.begin(), text.end());
ofstream reverse("copy.dat");
reverse << text;
orig.close();
reverse.close();
return 0;
}
Allocating a single char array instead of just using an automatic array is wasteful, and forgetting to delete it causes a memory leak, but a vanishingly small inefficiency compared to the O(N2) complexity of the main loop.
You should allocate get the filesize, allocate an array of char of said size and read it in one call to read.

C++ Program Crashes when method return value is assigned to an int

This problem is blowing my mind right now.
int main()
{
char inputChar;
char *buffer = nullptr;
int size = 0;
read(buffer); //this is the line causing problems...
int numberOfFrames = (size / MAX_FRAME_SIZE) + 1;
frame array[numberOfFrames];
for(int i = 0; i < size; i++)
{
buffer[i] = appendParityBit(buffer[i]);
}
constructFrame(buffer, size, array);
transmitFrames(array, numberOfFrames);
}
int read(char *buffer)
{
int fileSize;
ifstream myfile ("inputFile");
if (myfile.is_open())
{
fileSize = getFileLength(myfile);
buffer = new char[fileSize];
myfile.read(buffer, fileSize);
myfile.close();
}
return fileSize;
}
int getFileLength(ifstream &myfile)
{
myfile.seekg(0, ios::end);
int size = (int) myfile.tellg() - 1;
myfile.seekg(0, ios::beg);
return size;
}
now if i do a
cout << read(buffer);
on the line that is causing problems, i receive an integer back...great, perfect. but if i try to do
size = read(buffer);
my program crashes...i'm at a loss.
You are passing a variable by value (doesn't matter if it is a pointer or not). On the receiving end, the function makes a local copy of what is passed, works with the local copy, and poof, the local copy goes away when the function returns.
This occurs regardless of whether what you're passing is a pointer or not. For example, take this simple code:
void foo(int x)
{
x = 10;
}
int main()
{
int val = 0;
foo(val);
cout << val; // how come val is still 0 and not 10?
}
Note that val is still 0, even though the function is changing the parameter that is being passed. To fix this problem, you pass a reference to the value that will be changed:
void foo(int& x)
{
x = 10;
}
int main()
{
int val = 0;
foo(val);
cout << val; // now val is 10
}
With pointers, the rules don't change. You need to pass a reference to the pointer to have the change reflect back to the caller:
int read(char*& buffer)
{
int fileSize;
ifstream myfile ("inputFile");
if (myfile.is_open())
{
fileSize = getFileLength(myfile);
buffer = new char[fileSize];
myfile.read(buffer, fileSize);
myfile.close();
}
return fileSize;
}
Now the buffer in that function is not a local copy, but a reference to the variable you passed.
The other method (which is more "C" style) is to pass a pointer to the thing you want to change. You want to change the pointer value, so you pass a pointer to the pointer:
int read(char** buffer)
{
int fileSize;
ifstream myfile ("inputFile");
if (myfile.is_open())
{
fileSize = getFileLength(myfile);
*buffer = new char[fileSize];
myfile.read(buffer, fileSize);
myfile.close();
}
return fileSize;
}
// the caller
char *buffer;
//...
read(&buffer);
Of course, we have to change the syntax since it is a pointer that is being passed, thus we need to dereference it.
You are passing your buffer (char*) by value. Even when you allocate the buffer in your read() routine, this modifies local copy of the pointer. When you return from read(), you still have the old unititialized value of the pointer, which is not usable. To alleviate the issue, you can pass your buffer by reference.
I think the other answers have identified your coding error.
I note that you have tagged this as C++ ... and I suggest that perhaps your error would not have occurred if you used C++ features.
I have found the following (in SO and else where). It is similar, but relies on the strongly tested std::string memory management, and the file size requires no extra code on your part.
size_t read(std::string& buffer)
{
std::ifstream sIn("inputFile");
if (!sIn.is_open())
{
std::stringstream ssErr;
ssErr << "Can not open file '" << "inputFile" << "'" << std::endl;
throw ssErr.str();
}
std::stringstream ss(buffer);
ss << sIn.rdbuf(); // one line transfer of file contents
sIn.close(); // close sIn when we are done with it
if(sIn.bad())
throw "Err: sIn.rdbuf()";
return (ss.str().size());
}
Somewhere you might want a std::char array ... note that the c-style result (null terminated string) is available in buffer.c_str().
You can load any size text file (that fits in your memory) using this technique.

Access Violation error on VC++

Why am i getting "Access violation error reading " on the following program:
The error is on the while loop to read the file.
#include <iostream>
class fileReader
{
public:
FILE *fp;
char** lines;
fileReader()
{
fp = NULL;
}
fileReader(const char* path)
{
int i=0;
fp = fopen(path,"r");
while ( fgets(lines[i], 100, fp) )
i++;
}
};
int main(int argv, char** argc)
{
const char* path = "D:\\PS4263-2.txt";
fileReader *p = new fileReader(path);
for (int i=0; i<2; i++)
std::cout<<p->lines[i];
return 0;
}
EDIT
As mentioned by the answers I changed my code to (below), but I am still getting the same error.
#include <iostream>
class fileReader
{
public:
FILE *fp;
char** lines;
fileReader()
{
fp = NULL;
}
fileReader(char* path)
{
int j=0;
fp = fopen(path,"r");
if (fp == NULL)
return;
else
{
lines = (char**) malloc(sizeof(char *)*56000);
for (int i=0; i<56000; i++)
lines[i] = (char*)malloc(sizeof(char)*1440);
while ( fgets(lines[j], 1440, fp) )
j++;
fclose(fp);
}
}
};
int main(int argv, char** argc)
{
char* path = "D:\\testfile.txt";
fileReader *p = new fileReader(path);
for (int i=0; i<2; i++)
std::cout<<p->lines[i];
return 0;
}
There are a number of problems with this code. But primarily, the problem is that you're writing some evil C/C++ hybrid. Pick one of the two languages, and use that.
Here's a revised version of your code:
#include <iostream>
class fileReader
{
public:
FILE *fp;
char** lines;
fileReader() : fp(NULL) // initialization of members happens here
{
//fp = NULL; // anything here happens *after* initialization
lines = new char*[100]; // let's just assume max 100 lines. We have to allocate space for them
for (int i = 0; i < 100; ++i) {
lines[i] = new char[100]; // allocate space for the contents of each individual line
}
}
fileReader(const char* path)
{
lines = new char*[100]; // let's just assume max 100 lines. We have to allocate space for them
for (int i = 0; i < 100; ++i) {
lines[i] = new char[100]; // allocate space for the contents of each individual line
}
int i=0;
fp = fopen(path,"r");
while ( fgets(lines[i], 100, fp) )
i++;
}
~fileReader() {
// deallocate and close our members:
fclose(fp);
for (int i = 0; i < 100; ++i) {
delete[] lines[i]; // delete the contents of each line
}
delete[] lines; // delete the lines array
}
};
int main(int argv, char** argc)
{
const char* path = "D:\\PS4263-2.txt";
fileReader p(path); // don't use new unless you really really have to
for (int i=0; i<2; i++)
std::cout<<p.lines[i];
return 0;
}
Now at least it works, if each line contains less than 100 characters and there are fewer than 100 lines and the file exists and a dozen other conditions that we really should protect against. In particular, we spend a lot of effort on memory management: allocating and deallocating space for all the line data.
But we can do a lot better with just a few changes, if we actually start writing C++.
#include <iostream> // we need this for the standard streams (cout)
#include <fstream> // we need proper C++ file streams too
#include <string> // C++ has strings. Don't waste your time on char pointers
#include <vector> // C++ has a dynamic array class. Don't use pointers as ad-hoc arrays
class fileReader
{
public:
// FILE* fp; // no point in making this a class member, when it's only used in one function
std::vector<std::string> lines; // use a vector of strings. Much easier to manage
fileReader() // vectors are automatically initialized, no need to do anything
{
}
fileReader(std::string path)
{
std::ifstream fp(path); // create an input file stream
std::string result; // store the contents of the current line here
while (std::getline(fp, result)) {
lines.push_back(result); // append the resulting line to the end of the vector
}
}
};
int main(int argv, char** argc)
{
std::string path = "blah.txt";
fileReader p(path); // don't use new unless you absolutely have to
for (int i=0; i<2; i++)
std::cout<<p.lines[i];
return 0;
}
Note that we no longer have to manage our array memory. Vectors and strings automatically clean up after themselves when they go out of scope. And because we no longer use new to allocate the fileReader, it automatically gets deleted when it goes out of scope. This effectively starts a chain reaction where its members start cleaning up after themselves: the file stream closes, the vectors deallocates its memory after asking its stored strings to clean up and shut down. And the entire program folds over and closes down without us having to write a single line of code to handle it.
char** lines;
Was never allocated any memory!
To be able to do anything meaningful with it, You need to allocate it enough memory to hold the contents that you intend to hold in it.
Also on a sidenote,
You never deallocate the dynamic memory allocated to p by calling delete p; once done with its usage, this gives you an Undefined Behavior.
You never check for return value of standard library functions, You should always do so.
There are many problems in your codes:
char** lines has not been allocated. You need to allocate lines and
lines[i].
You never check if the file is really open. Check fp before using it.
You forgot to close the file pointer at the end. Call fclose(fp).
EDIT :
You are not deallocating lines, lines[i] and p. Be careful, you must use free() for lines and lines[i] and delete for p.