Manuel Gomez
Manuel Gomez

Reputation: 45

printf seems to be overwriting itself

So I'm trying to format this line to go

******************* Flight Reservation Request  **********************
 email name (M 1986/01/01)

but all it prints is

******************* Flight Reservation Request  **********************
 (M 1986/01/01)

The code that does this is

FILE * pFileCust; // stream Input for Customer Reservation data
void processCommandSwitches(int argc, char * argv[], char * * ppszCustomerFileName);
void processReservations();
int main(int argc, char * argv[]) {
    char * pszCustomerFileName = NULL;
    // Process the command switches
    processCommandSwitches(argc, argv, & pszCustomerFileName);
    // open the Customer Reservation stream data file
    if (pszCustomerFileName == NULL)
        exitError(ERR_MISSING_SWITCH, "-c");
    pFileCust = fopen(pszCustomerFileName, "r");
    if (pFileCust == NULL)
        exitError(ERR_CUSTOMER_RES_FILENAME, pszCustomerFileName);
    // process the Reservations
    processReservations();
    fclose(pFileCust);
    printf("\n"); // included so that you can put a breakpoint on this line
    return 0;
}
/****** you need to document and code this function *****/
void processReservations() {
    rewind(pFileCust);
    char gender[1];
    char dob[11];
    char emailAddress[53];
    char fullName[31];
    char address[31];
    char city[21];
    char state[31];
    char zip[61];
    fscanf(pFileCust, "%s %s %s %31[^\n]", gender, dob, emailAddress, fullName);
    printReservations(emailAddress, fullName, gender, dob, address, city, state, zip);
}
printReservations(char email[], char name[], char gender[], char dob[], char address[], char city[], char state[], char zip[]) {
    printf("******************* Flight Reservation Request  **********************\n");
    printf("%s %s (%s %s)\n", email, name, gender, dob);
}

input file

M 1986/01/01 [email protected] Pete Moss
123 Boggy Lane,New Orleans,LA,70112
H100.15005 2      
H222.15005 2
H200.15010 2
H333.15010 2       
END 0
M 1957/02/02 [email protected]______________________
456 Kernel,San Antonio,TX,78210
H222.15005 10
HXXX.XXXXX 10
H333.15010 5
END 0
F 1958/03/03 [email protected] Penny Loafer
789 Walking St,Philadelphia,PA,19102
H444.15001 1
H333.15010 1
END 0
M 1979/04/04 [email protected] Adam Sandler
444 Golf View,Hollywood,CA,92274
H100.15005 3
END 0
F 1989/05/05 [email protected] Melba Toast
222 Cracker Blvd,San Antonio,TX,78222 
H333.15010 2
H444.15015 2
END 0

Upvotes: 0

Views: 972

Answers (1)

Manuel Gomez
Manuel Gomez

Reputation: 45

Based on this suggestion by Retired Ninja:

You may have an embedded \r in the string. This can happen if you're reading a file created on Windows on an OS that doesn't use \r\n as a line end.

I changed my code from:

fscanf(pFileCust, "%s %s %s %31[^\n]", gender, dob, emailAddress, fullName);

to this:

fscanf(pFileCust, "%s %s %s %31[^\r\n]", gender, dob, emailAddress, fullName);

This fixed the problem and my output looks normal now.

Upvotes: 1

Related Questions