C Programming What does the below code segment do? #define RECORD_LENGTH 60 /* PORTFOLIO.DAT looks like the below: ibm 108.77 27.98 att 111.43 50 acdc 100 30 */ void main(void) { FILE *fptr; char stock_price_shares[RECORD_LENGTH + 1]; int i=0, count=0; // attempt to open PORTFOLIO.DAT for reading if ((fptr = fopen("PORTFOLIO.DAT", "r")) == NULL) printf("\nCannot open the file: PORTFOLIO.DAT\n"); else { while ( fgets(stock_price_shares, RECORD_LENGTH + 1, fptr) != NULL ) printf("\n%s", stock_price_shares); fclose(fptr); //close the file } printf("\nHow many new records for PORTFOLIO.DAT?"); scanf("%d ", &count); fptr = fopen("PORTFOLIO. DAT", "w"); /* writing */ for (i = 0; i < count; ++i) { gets(stock_price_shares); fputs(stock_price_shares, fptr); fclose(fptr); } }
C
What does the below code segment do?
#define RECORD_LENGTH 60
/*
PORTFOLIO.DAT looks like the below:
ibm 108.77 27.98
att 111.43 50
acdc 100 30
*/
void main(void)
{
FILE *fptr;
char stock_price_shares[RECORD_LENGTH + 1];
int i=0, count=0;
// attempt to open PORTFOLIO.DAT for reading
if ((fptr = fopen("PORTFOLIO.DAT", "r")) == NULL)
printf("\nCannot open the file: PORTFOLIO.DAT\n");
else {
while ( fgets(stock_price_shares, RECORD_LENGTH + 1, fptr) != NULL )
printf("\n%s", stock_price_shares);
fclose(fptr); //close the file
}
printf("\nHow many new records for PORTFOLIO.DAT?");
scanf("%d ", &count);
fptr = fopen("PORTFOLIO. DAT", "w"); /* writing */
for (i = 0; i < count; ++i) {
gets(stock_price_shares);
fputs(stock_price_shares, fptr);
fclose(fptr);
}
}
Step by step
Solved in 3 steps