file cutter :\

Pages: 12
Hi
I created a new project but it does not seems to be working.The program is running
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include<fstream>
#include<ctime>
#include<cstdlib>
#include<string>
static const char alphanum[] =
"0123456789"
"!@#$%^&*"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
using namespace std;

int stringLength = sizeof(alphanum) - 1;
double filesize(char filename[])
       {
       
           char ch;
	   double n;
           ifstream f;
	   f.open(filename,ios::in);
           while(!f.eof())
             {
               f>>ch;
               ch='\0';
                n++;
             }
         return n;
     }


char genRandom()  // Random string generator function. THIS IS NOT MY CODE
// I GOT THIS FROM cplusplus.com http://www.cplusplus.com/forum/windows/88843/
{

    return alphanum[rand() % stringLength];
}

char randgen()
{
    srand(time(0));
    char a;
    for(int z=0; z < 21; z++)
    {
	a=genRandom();

    }
    return a;

}
int main()
     {
     char ah[21];
     for(int t=0;t<25;t++)
           {
       ah[t]=randgen();
            }
     int flag=0;
     double n;
     char chara[50],i[21];
     cout<<"enter file name:";
     cin.get(chara,50,'\0');
     double c;
     c=filesize(chara);
     cout<<"enter number of files you want to cut it into:";
     cin>>n;
     int no=c/n;
     cout<<"\neach file would have "<<no<<" characters\n";
     cout<<n<<"+1 files will be created\n";
     fstream fc;
     fc.open(chara,ios::in|ios::app);
      double tell;
       fc.seekg(0);
      while(!fc.eof())
          {
	   while(flag!=no)
             {
             fstream fcs;
             strcpy(i,ah);
             fcs.open(i,ios::in|ios::app);
             fc>>c;
             fcs<<c;
              c='\0';
             ++flag;
             }
       memset(i,'\0',50);
	  
      }tell=fc.tellg();
   fc.seekg(tell);
while(!fc.eof())
{
fstream fx;
fx.open("extra",ios::in|ios::app);
fx.seekg(0);
fc>>c;
fx<<c;
}
fc.close();
cout<<"complete";
return 0;
   }



output


enter file name:Pragsis.txt
enter number of files you want to cut it into:5
each file would have 44 characters
5+1 files will be created


thats it no files of random name are being created.The program stops there.It is not showing
 completed 
and no new files are being created.Pragsis.txt already exists in the same directory and is about 1 kb in size
please help
Thanks for your time
Last edited on
1
2
3
     char chara[50],i[21];
     [...] 
     memset(i,'\0',50);


See anything there that looks wrong?
Thanks for the instant reply
sorry abt that I changed line to memset(i,'\0',21) .But that does not seems to be the problem and its still not working
Last edited on
Note that your for-loop on line 52 is running past the end of the array

The method you are employing to get the size of the file is skipping all whitespace because you are using the >> operator. To get the number of characters in a file, this code should do the trick:

1
2
3
4
5
6
7
std::streampos filesize(const char *filename)
{
    std::ifstream f(filename, std::ios::in | std::ios::ate);
    std::streampos size = f.tellg();
    f.close();
    return size;
}


The while loop on line 72 is looping until fc.eof() returns true which means that your loop on line 88 will never run

Overall, you are creating a lot of fstream objects and this will hurt the performance of your code in general.
Last edited on
Thanks for the reply THAT IS EXACTLY MY PROBLEM
but
The while loop on line 72 is looping until fc.eof() returns true which means that your loop on line 88 will never run

what does this mean I didint get you could you please explain.

I changed some parts of the program and now it seems to be creating only 1 file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include<fstream>
#include<ctime>
#include<cstdlib>
#include<string.h>
using namespace std;
static const char alphanum[] =
"0123456789"
"!@#$%^&*"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";

int stringLength = sizeof(alphanum) - 1;
streampos filesize(const char *filename)
{
     ifstream f(filename, ios::in|ios::ate);
     streampos size = f.tellg();
    f.close();
    return size;
}



char genRandom()  // Random string generator function.
{

    return alphanum[rand() % stringLength];
}

char randgen()
{
    srand(time(0));
    char a;
    for(int z=0; z < 21; z++)
    {
	a=genRandom();

    }
    return a;

}
int main()
     {
     char ah[21];
     for(int t=0;t<25;t++)
           {
       ah[t]=randgen();
            }
     int flag=0;
     double n;
     char chara[50],i[21],letter;
     cout<<"enter file name:";
     ciin.get(chara,21,'\0');
     double c;
     c=filesize(chara);
     cout<<"enter number of files you want to cut it into:";
     cin>>n;
     int no=c/n;
     cout<<"\neach file would have "<<no<<" characters\n";
     cout<<n<<"+1 files will be created\n";
     fstream fc;
     fc.open(chara,ios::in|ios::app);
      double tell;
       fc.seekg(0);
      while(!fc.eof())
                {
	       int count=0; // really I have no idea how this statement let the while loop ran atleast once
	   while(flag!=no)
             {
             fstream fcs;
             strcpy(i,ah);
             fcs.open(i,ios::in|ios::app);
             fc>>letter;
             fcs<<letter;
              c='\0';
             ++flag;
           
             }
       count++;
       memset(i,'\0',21);
      
	  
      }tell=fc.tellg();
   fc.seekg(tell);
while(!fc.eof())
{
fstream fx;
fx.open("extra",ios::in|ios::app);
fx.seekg(0);
fc>>letter;
fx<<letter;
}
fc.close();
cout<<"complete";
return 0;   
}


now output
enter file name:Pragsis.txt
enter number of files you want to cut it into:5
each file would have 37 characters
5+1 files will be created

but when I check out a new file of name HHHHHHH was seen to be created which had some info of pragsis.txt .But the loop seems to be running only once .How did this happened

thanks for the replys and time
Last edited on
std::ios::eof() returns true when end of file has been reached by a stream object.
http://www.cplusplus.com/reference/ios/ios/eof/

The while loop on line 72 depends on the fact that EOF has not been reached which is why you did while (!fc.eof()). The only time when the control leaves that while loop is when it's condition is false and when this happens, this means you have reached the end of the file in the current file you are reading.

On line 88, you set this same condition for the loop, but since this condition is already false from the last while loop, that line 88 is never executed.

Therefore, if the while loop on line 72 terminated, and the while loop on line 88 was never executed, you should have seen the "complete" message. So with that being the case that you did not see the message, the problem lies on the fact that the while loop on line 72 never terminated.

Hint:
74
75
76
while(flag!=no)
...
++flag;
Last edited on
Smac89 wrote:
when the EOF character has been read
There is no such character. In fact, EOF is defined to not be a character that could possibly be read from a stream.
Last edited on
29
30
31
32
33
34
35
36
37
38
39
40
char randgen()
{
    srand(time(0));
    char a;
    for(int z=0; z < 21; z++)
    {
	a=genRandom();

    }
    return a;

}

You shouldn't call srand multiple times (unless you know what you're doing, of course).
Call it just once in main. (The reason for this is: imagine that you call randgen() many times in a row (which you do in this case). Then the value of time(0) will likely be the same for each call (since the calls are happening so fast). Hence, you'll be seeding the random number generator with the same value over and over, and so you'll generate the same sequence of numbers each time.)

Also, your for loop in this function is somewhat pointless -- it just assigns to the same char variable over and over.
Thanks for the replay
ok will do that
Also, your for loop in this function is somewhat pointless -- it just assigns to the same char variable over and over.
. Thats correct the file names I get seems to be hhhhhhhh/HHHHHHHH/%%%%%%%%%
Smac89 wrote:
So with that being the case that you did not see the message, the problem lies on the fact that the while loop on line 72 never terminated.

If this is true I should have seen a large number of files in my folder but the trut is there is not even one also
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 while(!fc.eof())
                {
	       int count=0; //This statement made the while loop to execute //once 
//and create a single file that contaions about 35 characters as expected 
//BUT ONLY ONCE HOW???? COULD YOU PLEASE EXPLAIN
	   while(flag!=no)
             {
             fstream fcs;
             strcpy(i,ah);
             fcs.open(i,ios::in|ios::app);
             fc>>letter;
             fcs<<letter;
              c='\0';
             ++flag;
           
             }
       count++;
       memset(i,'\0',21);
      
	  
      }tell=fc.tellg();

I think in the answer of this question lies my problem
Last edited on
Did you look at the hint in my last comment? That should tell you why the inner loop ran only once
YES
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
while(!fc.eof())
          {
	   while(flag!=no)
             {
             fstream fcs;
             strcpy(i,ah);
             fcs.open(i,ios::in|ios::app);
             fc>>c;
             fcs<<c;
              c='\0';
             ++flag;
             }
       flag=0;          // ? is this  what you meant
       memset(i,'\0',50);
	  
      }


I am now seeing
completed
on my console screen but again no files are being created
Last edited on
I think in the answer of this question lies my problem

I think the you have a few problems contributing to your question. Here's one: You only ever generate one random file name. Here's another: If you open a file for input and it doesn't already exist, the open will fail.
I managed to fix your code, but I basically had to switch over to std::strings since they're so much easier for me to work with.
(Not that you have to use them, but....)

Anyways, I can't even keep track of what I changed, so I'll just point out some of the bigger issues:
-- You're trying to create n different files. That means you need n different (random) file names. Currently, you're only generating one (ah). By the way, that string isn't null-terminated -- you should probably fix that (a simple ah[20] = '\0' should do).
So, instead of generating a (single) random filename now, generate them as you go, when you need them. (More on that later.)

By the way, also see my other post above about the srand issue.

-- I would open your files (all of them) in binary mode (with ios::binary). (Fixed a minor issue with weird splits for me)

-- while(!fc.eof())
Don't loop on eof. You're trying to create n files, so make this a for loop that creates n files:
for (int i = 0; i < n; ++i)
At the beginning of this for loop would be a good place to generate a new random file name.
By the way, at least on my OS, the '*' (asterisk) character isn't a valid character in a file name, so you should probably remove that from your alphanum string (speaking of which, you can get the size of it by calling strlen -- don't rely on sizeof).

-- Instead of
1
2
fc>>letter;
fcs<<letter;

Do this:
1
2
fc.get(letter);
fcs.put(letter);
.
That's because fc >> letter will eat the whitespace, and you probably don't want that.
Same thing down below when you write to your "extra" file.

--
1
2
tell=fc.tellg();
fc.seekg(tell);

Not sure what this is for, but you don't need it.

-- By the way: you should probably check to make sure that you actually have a file open.
Thanks for the reply,
i Managed to do all that you said.......


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include<fstream>
#include<time>
#include<stdlib>
#include<string>
using namespace std;
static const char alphanum[] =
"0123456789"
"!@#$%^&"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";

int stringLength = sizeof(alphanum) - 1;
streampos filesize(const char *filename)
{
     ifstream f(filename, ios::in|ios::ate);
     streampos size = f.tellg();
    f.close();
    return size;
}



char genRandom()  // Random string generator function.
{

    return alphanum[rand() % stringLength];
}

char randgen()
{
    
    char a;
    for(int z=0; z < 21; z++)
    {
	a=genRandom();

    }
    return a;

}
int main()
     {
    srand(time(0));
     char ah[21];
     
     int flag=0;
     double n;
     char chara[50],i[21],letter;
     cout<<"enter file name:";
      gets(chara);
     double c;
     c=filesize(chara);
     cout<<"enter number of files you want to cut it into:";
     cin>>n;
     int no=c/n;
     cout<<"\neach file would have "<<no<<" characters\n";
     cout<<n<<"+1 files will be created\n";
     fstream fc;
     fc.open(chara,ios::in|ios::app|ios::binary);
      double tell;
       fc.seekg(0);
      for( int j=0;j<n;++j)
                {
	     
	   while(flag!=no)
             {
             ofstream fcs;
             for(int t=0;t<25;t++)
           {
          i[t]=randgen();
            }
             fcs.open(i,ios::app);
             fc(letter);
             fcs.get(letter);
              c='\0';
             ++flag;
           
             }
          flag=0;
      
       memset(i,'\0',21);
      
	  
      }tell=fc.tellg();
   fc.seekg(tell);
while(!fc.eof())
{
ofstream fx;
fx.open("extra",ios::app|ios::binary);
fx.seekp(0);
fc.get(letter);
fx.put(letter);
}
fc.close();
cout<<"complete";
return 0;

   }

It seems to be making the files all right but it's making lot of them.Each file containing a single character form the original pragsis.txt

pls help


Last edited on
That's because you're telling it to open a bunch of files.
Here's the pseudocode for what you should have:
for j = 0 to n-1
    Generate a new file name (set each value of i to a random character)
    Open that file
    while you haven't written 'no' characters yet
        Grab a character from the original file
        Write it to the new (currently opened) file
        Increment character count ('flag' in your case)

Here's what you wrote:
for j = 0 to n-1
    While you haven't written 'no' characters yet
        Generate a new file name (set each value of i to a random character)
        Open that file
        Grab a character from the original file
        Write it to the new file
        Increment character count

Well, hopefully that should give you an idea of how to fix it.

By the way, you could also use a for loop instead of the while(flag!=no) loop:
for (int flag = 0; flag != no; ++flag) // Though 'flag' seems to be a rather strange name...
That way, you wouldn't have to write ++flag; or flag = 0; manually yourself.
YOU MEAN LIKE THIS???
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

 for( int j=0;j<n-1;++j)
                {
	     
       for (int flag = 0; flag != no; ++flag)
             {
            for(int t=0;t<25;t++)
           {
          i[t]=randgen();
            }
             ofstream fcs;
             fcs.open(i,ios::app);
             fc>>letter;
             fcs<<letter;
              c='\0';
             
          }
        memset(i,'\0',21);
      }tell=fc.tellg();
   fc.seekg(tell);
while(!fc.eof())
{
ofstream fx;
fx.open("extra",ios::app|ios::binary);
fx.seekp(0);
fc>>letter;
fx<<letter;
}
fc.close();
cout<<"complete";
Not quite...
I meant to move the
7
8
9
10
11
12
for(int t=0;t<25;t++)
{
    i[t]=randgen();
}
ofstream fcs;
fcs.open(i,ios::app);

outside of the for (int flag = 0; flag != no; ++flag) loop.
Otherwise, you'd be opening a file for each character....
ok thanks it worked
I would open your files (all of them) in binary mode (with ios::binary)


If the objective is to split the file by the number of characters in the file, this is a bad idea. You need to handle possible character combinations for newline translations yourself in order to split files accurately. (The file sizes should not end up the same. The number of logical characters in the files should end up the same.)

One can change the default behavior of istream and operator>> with regards to whitespace via the ios_base format flags or the noskipws manipulator in <iomanip>.



Pages: 12