Results 1 to 6 of 6

Thread: Please look at this SIMPLE code

  1. #1

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    Exclamation Please look at this SIMPLE code

    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

    I have tried the following code but you can look at the result, and it is not what I am expecting

    Code:
    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
    Baaaaaaaaah

  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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."

  3. #3

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    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?
    Baaaaaaaaah

  4. #4
    Destined Soul
    Guest
    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.

  5. #5

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    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?
    Baaaaaaaaah

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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
  •  



Click Here to Expand Forum to Full Width