-
October 17th, 2006, 08:27 AM
#1
txt file size
Hi,
I read "data.txt" and max size is 775 kt, about 30 000 items.
I change all this red values
How i can read bigger file ?
Thanks
str_read = "data.txt";
char list[1000000];
ifstream inputData;
if(str_read != "") //Read box not empty
{
if((outfile = fopen(str_read ,"r+t")) == NULL)//for wrong file typed
MessageBox("Not find","error!");
else if((outfile = fopen(str_read , "r+t")) != NULL)
{
int numread = fread( list, sizeof( char ), 1000000, outfile );
//list[numread] =0 ;/*' '\;*/
//CString tmp;
inputData.open(str_read );
inputData.getline(list, 1000000, '\0');
char gen[30000];
int k=0;
for (int i=0; i<100000; i++)
{
if(k !=30000)
{
gen[k] = (char)list[i];
if(list[i] == '\n')
{
gen[k] = '\r';
k++;
gen[k] = '\n';
}
k++;
}
}
fclose( outfile );
-
October 17th, 2006, 08:43 AM
#2
Re: txt file size
Your question is not clear, but if you want to read files without knowing their size it better to check the file size and then allocate the buffer something like this:
Code:
HANDLE hFile =::CreateFile("c:\\File.txt",GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if ( hFile == INVALID_HANDLE_VALUE)
return;
DWORD dwFileSize =::GetFileSize(hFile,NULL);
if ( dwFileSize <= 0 )
return;
char *szBuf=new char[dwFileSize];
DWORD dwBytes=0;
::ReadFile(hFile,szBuf,dwFileSize,&dwBytes,NULL);
delete []szBuf;
::CloseHandle(hFile);
Cheers
-
October 17th, 2006, 08:44 AM
#3
Re: txt file size
I don't even want to figure out what you're trying to do there. The size of the file shouldn't affect how you go about reading it. If it's a txt file there are several tools available to make reading them easy. I use CStdioFile myself.
Also, if(str_read != "") isn't how you compare strings in C or C++.
-
October 17th, 2006, 08:45 AM
#4
Re: txt file size
Well you're posting in the c++ forum, so do it the c++ way Drop the fopens, freads, etc. And also be aware that when you read a line, if your line is in fact 1000000 characters long, then where are you going to put the null terminator in your array!? Also read at least 1 less character into your buffer to allow for the null terminator character.
But I digress, back to your issue! use ifstream/ofstream. Then read in your lines to a std::string instead of char[] with:
Code:
std::ifstream inputData("data.txt");
std::string str;
std::getline(inputData, str);
As you can see, I didn't specify any lengths or any char[] arrays. As a rule of thumb, if you think you need to use a char[] array, you're probably wrong Most things in the standard library, along with the STL, will keep you from having to use a char[] or char*;
Cheers
-eli
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|