Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. (ask) need help how to divide a file into some file

(ask) need help how to divide a file into some file

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialc++
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    moonstalker
    wrote on last edited by
    #1

    so here it is, I really dont know how to program here the problem : I have a file .txt format, in which there are many number sequentially down. for example : +1 +2 +3 +4 +5 etc... eg: my data have 100 number down, I have to divide them into 10 parts. later the file become 10 file .txt format, in which there are number 1 - 10 (part one), 11 - 20 (part two), 21 - 30, etc ... and the program must be made from C++. Thanks for help

    D R 2 Replies Last reply
    0
    • M moonstalker

      so here it is, I really dont know how to program here the problem : I have a file .txt format, in which there are many number sequentially down. for example : +1 +2 +3 +4 +5 etc... eg: my data have 100 number down, I have to divide them into 10 parts. later the file become 10 file .txt format, in which there are number 1 - 10 (part one), 11 - 20 (part two), 21 - 30, etc ... and the program must be made from C++. Thanks for help

      D Offline
      D Offline
      dusty_dex
      wrote on last edited by
      #2

      If this an excersise question you won't learn if you don't try and solve this yourself. here's a link to file io file io but if you haven't a clue, you should start at the tutorial introduction and work through some things until you feel confident to tackle the file splitting puzzle. Orphaned files are a nightmare, if you don't catch errors and close the file before your program exits correctly.

      "It's true that hard work never killed anyone. But I figure, why take the chance." - Ronald Reagan That's what machines are for. Got a problem? Sleep on it.

      M 1 Reply Last reply
      0
      • D dusty_dex

        If this an excersise question you won't learn if you don't try and solve this yourself. here's a link to file io file io but if you haven't a clue, you should start at the tutorial introduction and work through some things until you feel confident to tackle the file splitting puzzle. Orphaned files are a nightmare, if you don't catch errors and close the file before your program exits correctly.

        "It's true that hard work never killed anyone. But I figure, why take the chance." - Ronald Reagan That's what machines are for. Got a problem? Sleep on it.

        M Offline
        M Offline
        moonstalker
        wrote on last edited by
        #3

        ok, I will try thanks for qoute :)

        1 Reply Last reply
        0
        • M moonstalker

          so here it is, I really dont know how to program here the problem : I have a file .txt format, in which there are many number sequentially down. for example : +1 +2 +3 +4 +5 etc... eg: my data have 100 number down, I have to divide them into 10 parts. later the file become 10 file .txt format, in which there are number 1 - 10 (part one), 11 - 20 (part two), 21 - 30, etc ... and the program must be made from C++. Thanks for help

          R Offline
          R Offline
          ramrooney
          wrote on last edited by
          #4

          Hi moonstalker, Here is the solution for your problem,

          #include "stdafx.h"
          #include <string>

          int _tmain(int argc, _TCHAR* argv[])
          {
          FILE *fHnd = fopen("C:\\sample.txt", "r+");

          char \*pBuffer = new char\[5\];
          for (int nFileCnt = 0; nFileCnt < 10; nFileCnt++)
          {
              std::string strFileName = "";
              sprintf((char \*)strFileName.c\_str(), "C:\\\\File%d.txt", nFileCnt);
              FILE \*fHndWrite = fopen(strFileName.c\_str(), "a+");
              for (int nCnt = 0; nCnt < 10; nCnt++)
              {
                  fgets(pBuffer, 5, fHnd);
                  fwrite(pBuffer, 1, 3, fHndWrite);
              }
              fclose(fHndWrite);
          }
          fclose(fHnd);
          
          return 0;
          

          }

          In this code assuming you keep your numbers in the file "Sample.txt" file and after you run the code you will get 10 files generated with the names such as File0, File1 etc. Each of these files consist of 10 numbers. Bye.

          S 1 Reply Last reply
          0
          • R ramrooney

            Hi moonstalker, Here is the solution for your problem,

            #include "stdafx.h"
            #include <string>

            int _tmain(int argc, _TCHAR* argv[])
            {
            FILE *fHnd = fopen("C:\\sample.txt", "r+");

            char \*pBuffer = new char\[5\];
            for (int nFileCnt = 0; nFileCnt < 10; nFileCnt++)
            {
                std::string strFileName = "";
                sprintf((char \*)strFileName.c\_str(), "C:\\\\File%d.txt", nFileCnt);
                FILE \*fHndWrite = fopen(strFileName.c\_str(), "a+");
                for (int nCnt = 0; nCnt < 10; nCnt++)
                {
                    fgets(pBuffer, 5, fHnd);
                    fwrite(pBuffer, 1, 3, fHndWrite);
                }
                fclose(fHndWrite);
            }
            fclose(fHnd);
            
            return 0;
            

            }

            In this code assuming you keep your numbers in the file "Sample.txt" file and after you run the code you will get 10 files generated with the names such as File0, File1 etc. Each of these files consist of 10 numbers. Bye.

            S Offline
            S Offline
            Shaheed Legion
            wrote on last edited by
            #5

            I agree with dusty_dex, the poster should have attempted a solution first, instead of being provided a cut and dry answer to his homework. This is not a forum for quick homework answers, and should be kept clear of students who are too lazy to attempt solutions to their homework. If the poster had provided sample code and asked for bug hunting help, then it would have been a different matter entirely. :doh:

            For awesome websites or just to chat check out my blog for more info. . .

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups