|
-
Aug 2nd, 2001, 02:17 AM
#1
Thread Starter
PowerPoster
-
Aug 2nd, 2001, 03:33 AM
#2
Frenzied Member
This line is the problem:
Code:
myfile = (filenom) + i;
You can't add an int to a char*. You want to concatenate the fime number and the counter, and make a new string out of it. To do that you need to convert the int into a string using the itoa() function and then concatenate them using strcat(). Alternatively, and much more easily, you can use the sprintf() function, like this:
Code:
sprintf(myfile, "%s%d", filenom, i);
But you'll need to allocate a buffer for the string myfile when you declare it:
Code:
// used to be: const char *myfile = NULL
// now it should be:
char myfile[50]; // 50-byte buffer allocated
Harry.
"From one thing, know ten thousand things."
-
Aug 2nd, 2001, 12:27 PM
#3
Thread Starter
PowerPoster
Thanks a lot!
Just one more thing:
What if I want to create the # of number use wants (eg 150 files)
Do I need to change to myfile buffer or I just change the value of i, and it will work file?
-
Aug 2nd, 2001, 01:23 PM
#4
Yes, increasing the limit of i should work. If you were to pass an integer "int someNumber" to the function, then you would use:
Code:
for (i = 1; i < someNumber; i++)
{
sprintf(myfile,"%s%d",filenom,i);
files[i] = fopen(myfile, "w+");
cout<<myfile<<endl;
fclose(files[i]);
}
Hope this helps
Destined.
-
Aug 2nd, 2001, 02:16 PM
#5
Thread Starter
PowerPoster
Ok, that was working just one more:
I have created the files in a loop but how do open a certain file like "a.txt" and read 10 characters at a time, and write these characters to the files that I create in a loop like this:
file1 ----> 0 - 10 chars from a.txt
file2 ----> 11 - 20 chars from a.txt
file3 ----> 21-30 chars from a.txt
....and so on
Please?
-
Aug 2nd, 2001, 04:35 PM
#6
Monday Morning Lunatic
This is code for a file splitter program I made once. It won't compile because a few of the classes are missing, but the theory's sound 
Code:
#include <iostream>
#include <cmath>
#include <mjp/DirectFile.h>
#include <mjp/Defines.h>
#include <mjp/Tools.h>
#define BUF_SIZE 65536
using namespace std;
long round(float value) {
// Round to nearest integer
if((long)value == (long)(value + 0.5f))
return (long)value;
else
return (long)(value+1);
}
void main(int argc, char **argv) {
// splitter.exe file.ext partsize_kilobytes
ulong ulPartSize;
ubyte pubBuf[BUF_SIZE];
ulong ulCount = 0;
ulong ulTotal = 0;
ulong ulNumParts = 0;
DirectFile dfIn;
DirectFile dfOut;
if(argc < 3) {
cout << "Usage: " << argv[0] << " filename partsize" << endl;
return;
}
ulPartSize = atoi(argv[2]) * 1024; // size in bytes
dfIn.OpenRead(argv[1], true);
// Find number of parts
// This statement adds one extra for the incomplete part
ulNumParts = round((((float)(dfIn.Length())) / ((float)ulPartSize))) + 1;
cout << "Number of parts = " << ulNumParts << endl;
for(ulong j = 1; j <= ulNumParts; j++) {
dfOut.OpenWrite((string(argv[1]) + "." + mjpTools::Long2String(j)).c_str(), true, true, false);
for(ulong i = 0; i < ulPartSize; i) {
ulTotal = ulPartSize - i;
if(ulTotal >= BUF_SIZE) ulTotal = BUF_SIZE;
ulCount = dfIn.Read(pubBuf, sizeof(ubyte), ulTotal);
if(ulCount == 0 || ulCount == -1) break;
dfOut.Write(pubBuf, sizeof(ubyte), ulCount);
i += ulCount;
}
dfOut.Close();
}
dfIn.Close();
}
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
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
|