Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

how about this for simplification: I have to read from a text file. And then I p

ID: 3639441 • Letter: H

Question

how about this for simplification: I have to read from a text file. And then I put whatever I read from that text file in a character array called bufferD, And now what I have to do is to break down the data from my character array: bufferD, into segments and put it into a new buffer called BufferT.

The segments should look like 30 zeros+58 characters read from the bufferD. In other words this is what it should be like when I put my segments into the bufferT: 0000...0000HelloWorld. The blah..0000...0000_______
Those 58 characters from a text file will include even the new line and spaces.
I didn't have any problem including these characters(new line and spaces and period, etc.)

So far i just tried using string functions. Let's say that what I read from a text file consist of 192 characters. I know that when I divide 192 by 58 i should get about 4 segments. My 1st segment would contain 30 zeros + 1st 58 characters from my text file. 2nd segment would contain 30zeros + characters 59 through 116 and so on.

My problem is I don't know how to create a loop within a loop. Because my program works only for the first 58 characters of from bufferD. I dont know how to detect from characters 59 to the end of bufferD. So far I have this:

char *Layer3(char *bufferD)
{
char *bufferT = (char *) malloc(4400); //I'm allocating space for my data
char tempbuffer[30+MSS+1] = "000000000000000000000000000000"; //Place holder for 30 zeros + 58 data characters. and MSS is define as 58.

char temp2buffer[MSS+1];
size_t e = strlen(Dbuffer); //The size of sendfile.
double a = ceil((double) e / 58); //The number of segments for sendfile.

strncpy(temp2buffer, Dbuffer, MSS+2); //Contains the first 58 characters from bufferD
strcat(tempbuffer, temp2buffer); //Contains 1segment 30zeros+58characters

strcpy(Transbuffer, ""); //Should contain all my data segments
strcat(Transbuffer, tempbuffer); //Contains my 1st segment.
}

So far this is what I have. I'm sure there's a way to do create a segment from bufferD without doing all my steps. But the step I have works fine. It was just a matter or continuing to do it for the rest of the characters from bufferD.

Would anyone be able to look at this and give me some advice of what I could do?

Explanation / Answer

Simple, straight forward solution

#include <stdio.h>

#include <stdlib.h>

using namespace std;
int main(){

char myChars[89];
FILE *pFile;

int c;   

pFile = fopen ("junk.txt","rb");

if (pFile==NULL) {

printf("Error opening file");

exit(1);

}

while(true) {

for(int i=0; i<30; i++)

myChars[i]='0';

for(int i=0; i<58; i++) {

c = fgetc(pFile);

if(c != EOF)

myChars[i+30] = c;

else {

//Reached end of file, print the remaining,close file and return

myChars[i] = ' ';

printf("%s", myChars);

fclose(pFile);

return 0;

}

}

myChars[88] = ' ';

printf("%s", myChars);

}

//Should not reach here

return 0;

}