(ask) need help how to divide a file into some file
-
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
-
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
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.
-
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.
ok, I will try thanks for qoute :)
-
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
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.
-
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.
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. . .