Everything returns NULL

closed account (4Gb4jE8b)
Ok so i've tried read(), get(), and getline(), to try and get the contents of my file as a whole in one variable string/char*/stringbuf, Each time buffer (the variable name) returns NULL, or more appropriately absolutely nothing at all. What am I doing wrong?

Below are copies of get() and getline(), read is very similar to getline, just without the "last" part of the argument.

getline
1
2
3
4
5
6
7
8
9
10
	char* buffer;
	int last;
	fin.open(Parent_file.c_str());
	fin.seekg(0, ios::beg);
	last = ceil(end);
	buffer = new char [last];
	fin.getline(buffer,last,EOF);
	fin.close();
	cout << "buffer is... '" << buffer << "'\n";
	pause();


get
1
2
3
4
5
6
7
	stringbuf buffer;
	fin.open(Parent_file.c_str());
	fin.seekg(0, ios::beg);
	fin.get(buffer,EOF);
	fin.close();
	cout << "buffer is... '" << buffer.str() << "'\n";
	pause();


again both return null and i've no idea why. I know some of you have probably seen my threads most representing some similar idea of this, and i don't mean to double post, i just feel this is a different enough explanation to justify a new thread.
In the first, where do you get end from?
I would use mmap for this.
closed account (4Gb4jE8b)
my apologies, i forgot that end is just the size of the file in bytes.

After looking around on the web for quite some time now, I still don't understand mmap, does it only work for Unix/Linux? I'm working on windows vista.

Can you explain or point me in the right direction of a break down of mmap?
mmap is provided by the POSIX standard. It will work on windows only if you are using gcc or MinGW. Visual C++ does not have it. Therefore, you should figure out a way to just load the whole file into memory. To do this, I reccommend the stdio.h fread() function, like this:
1
2
3
4
5
6
FILE*f=fopen("the file.dat","rb");
fseek(f,0,SEEK_END);
int siz=ftell(f);
fseek(f,0,SEEK_SET);
char *data=malloc(siz);
if(fread(data,1,siz,f)!=siz)perror("Couldn't read the file"); //should also do some cleanup. 

closed account (4Gb4jE8b)
this is causing a system error when setting a stringstream or string equal to data (for parsing). The error is in the last two lines (according to the error message)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
	char *buffer;
	FILE*f=fopen(Parent_file.c_str(),"rb");
	fseek(f,0,SEEK_END);
	buffer=(char*)malloc(end);
	if(fread(buffer,1,end,f)!=end)
	{
		cout << "Couldn't read Parent File\n";
	}
	cout << "buffer is... '" << buffer << "'\n";
	pause();

	for(x = 0;x<number;x++,y++)
	{
		//Writing the file names
		ostringstream child_file;		//allows for dynamic manipulation of strings
		stringstream text1;
		string in_text,out_text;
		int endp,p_endp,last_char,p_last_char;
		endp = 4096*y;p_endp = 4096*x;
		child_file << std_name << " Pt " << y << ".txt";
		child_name = child_file.str();
		fout.open(child_name.c_str());
		if(fexists(child_name.c_str()) == false)
		{
			cout << "\nIn file " << child_name.c_str() << "\n";
			cout << "Error: file unable to be made correctly\n";
			pause();
			return 1;
		}

		//Writing the file content
		text1 << buffer;
		in_text = text1.str();
Last edited on
Oh. Yeah, you need to put a zero byte at the end of the buffer.
Also, remember to seek back to the beginning of the file before reading.
change line 4 to buffer=(char*)malloc(end+1);
and add buffer[end]=0; after the read operation.
Last edited on
closed account (4Gb4jE8b)
I did all of that, but now i have a new error saying that in fseek.c stream!=null. Because this is an error, i'm presuming that means again the file is returning null.

ugh, why is everything returning null?

this same error occurs in the fread.c file when both fseek commands are removed
Last edited on
The fseek commands are very important, they manipulate where you are reading from. Also, check the actual file in notepad. Does it even exist?
closed account (4Gb4jE8b)
yes the files exist. And i understand what you're saying when you say they are very important, seek commands in general (istream::seek for example) have the same general properties and importantness :P

Though i wasn't able to solve my problem doing what you said to, I was able to find a solution using a while loop and a stringstream object.

1
2
3
4
5
6
7
8
9
char c;
	stringstream buffer;
	while (fin.good())		// loop while extraction from file is possible
	{
		c = fin.get();		// get character from file
		buffer << c;
	}
	fin.close();
	pause();


This puts the entire object into buffer, which can then be manipulated with the .str() component.

thanks for all your help (and to anyone who helped me in previous threads)
Topic archived. No new replies allowed.