any help pls

Hi,
I wrote a functions to cut a given file to any number of pieces.It's working perfectly but then as a continuation I also wrote a function to put back the file to original form which has some problems....The contents of the file to cut are read and copied to different files of randomly generated name.The record of names are kept in another textfile called Logfile.TXT.The function toput back the file is creating a file called "ORIGINALFILE".But the contents of ORIGINALFILE are not same as the first one.The first few lines are correctly there but the rest are blank space....
HERE IS THE CODE
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//// TO GENERATE RANDOM STRING
static const char alphanum[] =
"0123456789"
"!@#$%^&"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";


int stringLength = sizeof(alphanum) - 1;
///TO GET THE SIZE OF A FILE
streampos filesize(const char *filename)
{
     ifstream f(filename, ios::in|ios::ate);
        if(!filename)
             cout<<"\nerror required filename not found";
    streampos size = f.tellg();
    f.close();
    return size;
}


////// TO GENERATE RANDOM STRING
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;

}
// FUNCTION TO CUT THE FILE
void filecutter()
     {
            srand(time(0));
            ofstream log;
            log.open("LOGFILE.txt",ios::out);
            double n;
            char chara[50],i[8],letter;
            cout<<"enter file name:";
            gets(chara);
            double c;
            c=filesize(chara);
            cout<<"file has "<<c<<" characters\n";
            cout<<"enter number of files you want to cut it into:";
            cin>>n;
            long int no=c/n;
            cout<<"\neach file would have "<<no<<" characters\n";
            cout<<n<<" 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)
                {
                    for(int t=0;t<8;t++)
                           {
                             i[t]=randgen();
                          }	
                   ofstream fcs;
                   fcs.open(i,ios::app);     
                   for (long int flag = 0; flag<no;flag++)
                       {
                            fc.get(letter);
                            fcs.put(letter);
                            letter='\0';
	            cout<<flag<<" character out of "<<no<<" wrote to "<<i<<"\n";
                      }
                  log<<i;
                  memset(i,'\0',8);
                  cout<<j<<"/"<<n<<" files created\n";
                  fcs.close();
             }
       ofstream fx;
       fx.open("EXTRA",ios::app|ios::binary);
       while(!fc.eof())
               {
                   fc.get(letter);
                   fx.put(letter);
                }
       fc.close();
       cout<<"complete";
       log.close();
       ofstream lsize;
       lsize.open("LOGSIZE.txt",ios::out);
       lsize<<c<<" "<<no<<" "<<n;
       lsize.close();

  }
//TO PUT BACK THE FILES FROM THE AVAILABLE PIECES
void filehealer()
    {
      char ch,fname[8];
      double i,n;
      double filesize;
      long int ofilesize;
      ofstream original;
      original.open("ORIGINALFILE",ios::out|ios::binary);
      ifstream lsize;
      lsize.open("LOGSIZE.txt",ios::in);
      lsize.seekg(0);
      lsize>>filesize;
      lsize>>ofilesize;
      lsize>>n;
      cout<<"original file size :"<<filesize<<"\n";
      cout<<"part size:"<<ofilesize<<"\n";
      cout<<"no of parts:"<<n<<"\n";
      getch();
      ifstream log;
      log.open("LOGFILE.txt",ios::in);
      
      while(!log)
          {
            cout<<"\n logfile not found\n";
            getch();
             exit(0);
            }
      for(i=0;i<n;++i)
            {
             log.get(fname,9);
             cout<<fname<<"\n";
             getch();
             ifstream parts;
             parts.open(fname,ios::in);
             if(!parts)
                 {
                       cout<<"file/files not found aborting";
                       getch();
                       exit(0);
                 }

             parts.seekg(0);
             for(long int j=0;j<ofilesize;j++)
                     {
                     parts.get(ch);
                     original.put(ch);
                     ch='\0';
                      cout<<j<<"\nbytes++";
                    }
               memset(fname,'\0',8);
                parts.close();
	     cout<<i<<"/"<<n<<" files created\n";
            }
    ifstream extra;
    extra.open("EXTRA",ios::in);
    while(!extra.eof())
        {
            extra.get(ch);
           original.put(ch);
            ch='\0';
       }
  extra.close();
  lsize.close();
  log.close();
  original.close();
  cout<<"completed";
 }



Could some one please help..
Last edited on
At line 69, fcs.open() expects i to be a null-terminated string. You haven't terminated it. To fix this, define i to be char[9] and set i[8] = 0;. Also, it would be helpful if you renamed i to be something like fname. i is usually a small integer.

You will also need to change fname in filehealer() to be char[9]
No it's still not working and is producing the same bug.As I mentioned earlier the function filecutter() is working perfectly....Pls help
any help????
What dhayden said.

In filecutter(): A valid file name is only generated by accident.

On line 128: 9 is out of bounds. That may cause crash/undefined behavior

If I change line 128: log.get(fname,8); it shows
 file/files not found aborting
when i try
The reason for this is the problem with the file names in filecutter()
ya and that is....
I think you're missing what the others are telling you.

You are defining your arrays with 8 characters.
ex. char fname[8];

You are then asking to read 9 characters from the file, and store them into a container that can only hold 8 things (line 128). That has undefined consequences since you are now accessing memory out of the bounds of your array. You are potentially overwriting something else in memory that you need.

If you are lucky, you might get one or two of those files on accident, but you certainly aren't guaranteed to get them all. If the files are all 9 characters long, then you need to change fname to be 9 characters.

Also, to reiterate what dhayden mentioned in your filecutter code, these are C style strings. A string effectively needs to be one character larger than what you want so you can add a null terminator.

ex. "Hello World" is actually stored as "Hello World\0". That's how the program knows where the end of your string is. The trailing '\0' or char(0) is a null terminator. Without that, the program might read extra characters from memory that it thinks are part of your string. The end result is that your program might work, and it might not. But it probably won't be consistent, and is prone to segmentation faults.

To fix your code, just make your strings larger and put null terminators at the end whenever possible.
so what you are basically saying is that I should add '\0' to the end of all the random file names generated before writing them to log file ,Right?
The bigger issue is that your char arrays are not big enough to hold what you are squeezing into them. An 8 character array cannot hold 9 characters. The secondary problem is that C-style strings need to be null terminated or you can get unpredictable results. The null takes up space in the array, so if you want an array to hold 9 characters, you need to make it one larger than that to also hold the null terminator. Otherwise you can't know where the end of the string is. There is no size property on those strings, so functions typically loop through the characters until a null is found.
Last edited on
so what do you propose I do.I tried everything I can by kinda trial and error method
so on line 47:char chara[50],i[9],letter; //9 insted of 8 for null charcter
line 64:
1
2
3
4
5
6
{
                    for(int t=0;t<8;t++)
                           {
                             i[t]=randgen();
                          }
                 i[9]='\0';   // the character is null terminated 

line 101:char ch,fname[9]; //9 insted of 8
These are the suggested improvements and I tried it but still not working.Am I missing any more improvements here.But on logfile the null teminated charcater is stored as r at the end of each file name.So its causing to give out
file/files not found aborting
.And if I manually edit the logfile and run the program again its causing the first told error
contents of ORIGINALFILE are not same as the first one.The first few lines are correctly there but the rest are blank space....
Last edited on
Remember that arrays are indexed starting from 0. So for an array with 9 elements, the last one is at index 8. Thus the change at line 64 should be:
1
2
3
4
5
6
{
       for(int t=0;t<8;t++)
      {
             i[t]=randgen();
      }
      i[8]='\0';   // the character is null terminated 


dhayen:yes corrected that and still the 2nd problem(the main one) is still there

contents of ORIGINALFILE are not same as the first one.The first few lines are correctly there but the rest are blank space....
Last edited on
I hope you get the principle of c-strings now. The terminating '\0' is always required.

if there's something wrong with your "LOGSIZE.txt"/ofilesize it will of course fill the file with invalid characters ('\0') in the loop on line 141.

Line 70: You assume that the file can be split in equal parts. But what if that isn't true. Especially for the last part


By the way: The loops on line 154/84 will give an extra character.

coder777 wrote ::Line 70: You assume that the file can be split in equal parts. But what if that isn't true. Especially for the last part for the last part

Thats why I added an extra file extra to the program.Even after reading all the character if one is left it will be written to that file and if none is available it will be empty.
And by the way I found out another method to make name insted of random characters I would add numbers to it eg: if file to be cut is a.txt then the resulting files would be a1.txt,a2.txt....I dont know if it solves
contents of ORIGINALFILE are not same as the first one.The first few lines are correctly there but the rest are blank space....
problem still...
Last edited on
Can you post the current state of your code, please?
I just started to work on it.I dont know if I could finish it.My college starts day after tomorrow.
Last edited on
Topic archived. No new replies allowed.