CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: txt file size

  1. #1
    Join Date
    May 2003
    Location
    Turku/Finland
    Posts
    77

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

  2. #2
    Join Date
    May 2005
    Posts
    4,954

    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&#091;dwFileSize&#093;;
      DWORD dwBytes=0;
      ::ReadFile(hFile,szBuf,dwFileSize,&dwBytes,NULL);
      delete &#091;&#093;szBuf;
      ::CloseHandle(hFile);
    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

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

  4. #4
    Join Date
    Apr 2002
    Location
    PA, USA
    Posts
    1,658

    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
    =--=--=--=--=--=--=--=--=--=--=--=--=--=
    Please rate this post to show your appreciation for those that helped you.

    Before You Post A Question, Please Read This: How & When To Ask Your Question
    =--=--=--=--=--=--=--=--=--=--=--=--=--=

    -eli
    http://www.toad-software.com
    http://www.dailymission.com - Do It Daily

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured