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.
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.
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.
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)
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.
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
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)