Cannot Pass String to Main to Print
Hi guys, I read the FAQs and searched and could not find anything I think about this problem. If I did please forgive me and send me a link to it :(
Basically I am trying to enter an address into the program for which it will save into a file (I chose a csv) then when reran the program is suppose to print the content. Problem is when I print the string sAddress after I enter it it comes out correct (123 Elm Street). When I reenter the program and call it from the saved file I get "123" as the address and "Elm" as the phone number.
How can I make it so that I get the whole string in the address display?
Code:
#include <stdio.h>
#include <stdlib.h>
void newEntry (char fName[20], char lName[20], char sAddress[35], char pNumber[15]) {
FILE *pWrite;
FILE *pRead;
printf("Enter First Name: ");
scanf("%s", fName);
printf("Enter Last Name: ");
scanf("%s", lName);
fflush(stdin);
printf("Enter Address: ");
gets(sAddress);
printf("Enter Phone Number (9 Digits, No Symbols): ");
scanf("%s", pNumber);
printf("%s", sAddress);
pWrite = fopen("address.csv", "a");
if ( pWrite != NULL ) {
fprintf(pWrite, "%s %s %s %s", fName, lName, sAddress, pNumber);
fclose(pWrite);
}
else
printf("\nFile Cannot Be Opened\n");
}
int main (void) {
//variable declarations
int selection;
char fName[20] = {'\0'};
char lName[20] = {'\0'};
char sAddress[35] = {'\0'};
char pNumber[15] = {'\0'};
FILE *pWrite;
FILE *pRead;
//variable initialization
// Get selection
printf("Please Enter Selection: ");
printf("\n1. New Entry: ");
printf("\n2. Display Entries: ");
scanf("%d", &selection);
if ( selection == 1 ) {
newEntry (fName, lName, sAddress, pNumber);
}
else if ( selection == 2 ) {
pRead = fopen("address.csv", "r");
if ( pRead != NULL ) {
printf("\n Address Book\n");
while ( !feof(pRead) ) {
fscanf(pRead, "%s %s %s %s", fName, lName, sAddress, pNumber);
if ( !feof(pRead) )
printf("\n%s %s %s %s", fName, lName, sAddress, pNumber);
}
printf("\n");
}
else
printf("\nFile Not Opened\n");
}
else
printf("\nInvalid Selection\n");
system("pause");
return 0;
}