Click to See Complete Forum and Search --> : Please look at this SIMPLE code
abdul
Aug 2nd, 2001, 02:17 AM
it is not simple for me but I am sure that it is all simple for you. I want create 4 or 5 files and name then like:
filenumber1 filenumber2 filenumber3 .....
I can create then individually but that is just a dirty way to do it:D
I have tried the following code but you can look at the result, and it is not what I am expecting
Code:
char buffer;
char filenom[] = "filenumber";
const char *myfile = NULL;
int i;
FILE *files[5];
for(i = 1; i < 5; i++)
{
buffer = (int)i;
myfile = (filenom) + i;
files[i] = fopen(myfile, "w+");
cout<<myfile<<endl;
fclose(files[i]);
}
---------------------------------------------------------------------------------
Result:
It is just making 3 files with the names:
1) ilenumber
2)lenumber
3)lnumber
Why am I getting this result and how can I make the files with the correct name?
Please don't ignore
:confused:
HarryW
Aug 2nd, 2001, 03:33 AM
This line is the problem: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:
sprintf(myfile, "%s%d", filenom, i);
But you'll need to allocate a buffer for the string myfile when you declare it:// used to be: const char *myfile = NULL
// now it should be:
char myfile[50]; // 50-byte buffer allocated
abdul
Aug 2nd, 2001, 12:27 PM
Thanks a lot!:D
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?
Destined Soul
Aug 2nd, 2001, 01:23 PM
Yes, increasing the limit of i should work. If you were to pass an integer "int someNumber" to the function, then you would use:
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.
abdul
Aug 2nd, 2001, 02:16 PM
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?
parksie
Aug 2nd, 2001, 04:35 PM
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 :)#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();
}
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.