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

I have a a function that asks for user to create a file. After it is created, I

ID: 3813950 • Letter: I

Question

I have a a function that asks for user to create a file. After it is created, I open the file in binary mode "wb".

I then ask the user again to input a what text they'd like to put in the file using fwrite. BUT, I need to exclude the fact the newline character from being added to the file.

Also say that the maxium characters that the user can input is 160 characters. If they input more than 160 characters, I will go ahead and take the first 160 characters including the null pointer character and then flush/delete the rest of the user input. How do I then clear/flush the stdin so the next time I call it, it is empty/flushed??

void pack()

{

FILE *pack; //file to write in

unsigned char convert[MAXWORDS]; //buffer with 120

char buffer[MAXWORDS]; //buffer with 160

char hold[20]; //buffer for file name

printf("Enter a file name to save to "); //asks for file name

fgets(hold, 20, stdin);

sscanf(hold, "%s", hold);

  

pack = fopen(hold, "wb"); //open file in write mode

if(!pack) //check if file is made

{

printf("File not created ");

}

  

printf("Enter line of text to pack and save "); //asks user for a message

unsigned int i = 0;

fgets(buffer, sizeof buffer-1, stdin); //buffer will hold user message

while(i < sizeof buffer -1){ //while there are still chars, keep going.

//but stop before the newline for fgets

convert[i] = CharToSMS(buffer[i]); //convert the ASCII values

i++; //and save it to the placeholder from buffer

}

char packed[MINWORDS]; //this array is where the the bits are packed.

//for the new ASCII values.

PackArray(packed, sizeof packed, convert, sizeof convert); //method that packs

fwrite(packed, 1, sizeof packed, pack);

//fputs(packed, pack); //place the packed array to the file.

fclose(pack);

}

Explanation / Answer

code: (read comments for explanation)

void pack()
{   
FILE *pack; //file to write in
//char convert[MAXWORDS]; //buffer with 160   
char buffer[MAXWORDS]; //buffer with 160   
char hold[20]; //buffer for file name
//continuous loop till file is created
while(1)
{
printf("Enter a file name to save to "); //asks for file name
fgets(hold, 20, stdin);
  
pack = fopen(hold, "wb"); //open file in write mode   
if(pack == NULL) //check if file is made
{   
printf("File not created ");
}
else
{
break;
}
}
printf("Enter line of text to pack and save "); //asks user for a message
unsigned int i = 0;
fgets(buffer, MAXWORDS - 1, stdin); //buffer will hold user message
int size = strlen(buffer);
//removing from buffer if it is read
if(buffer[size-1] == ' ')
buffer[size-1] = '';
/*while(i < sizeof(buffer -1))
{ //while there are still chars, keep going.
//but stop before the newline for fgets
//convert[i] = CharToSMS(buffer[i]); //convert the ASCII values
i++; //and save it to the placeholder from buffer
}*/
//char packed[MAXWORDS]; //this array is where the the bits are packed.
//for the new ASCII values.
//PackArray(packed, sizeof(packed), convert, sizeof convert); //method that packs
fwrite(&buffer, sizeof(char), sizeof(buffer), pack);
//fputs(packed, pack); //place the packed array to the file.
fclose(pack);
fflush(stdin); //assuming fflush would work on your pc
/* if fflush doesn't work
int c;
while ((c = getchar()) != ' ' && c != EOF);
*/
  
}