Hi im new..
I am trying to create a program that is able to write to a file...
at first i started like code below
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
void wrecords(FILE *fp);
void fpfs(FILE *fp, char prom[], char str[], int choice);
void rrecord(FILE *fp);
int main(int argc, char *argv[])
{
FILE *fp;
int option;
fp = fopen("file.cvs", "a+");
printf("Write and Read File Experment\n\n");
for(;;) {
option = 0;
printf("1: Write Records\n");
printf("2: Read Records\n");
printf("3: Exit Program\n\n");
while(option == 0) {
printf("Please Choose Option: ");
scanf("%d", &option);
if(isdigit(option))
printf("Error Number Wasn't Entered\n");
else if(option < 1 || option > 3)
printf("invalid choice\n");
}
switch(option){
case 1:
wrecords(fp);
break;
case 2:
rrecord(fp);
break;
}
if(option == 3)
break;
}
fclose(fp);
printf("File has been closed, end of program\n");
getchar();
}
void wrecords(FILE *fp)
{
int id = 0;
char firstname[10][50];
char lastname[10][50];
if(fp == NULL){
printf("Error opening file\n");
exit(1);
}
for(id = 0; id < 5; id++) {
printf("Record #%d\n", id+1);
fprintf(fp, "%d,", id);
printf("Firstname: ");
scanf("%s", firstname[id]);
fprintf(fp, "%s,", firstname[id]);
printf("Lastname: ");
scanf("%s", lastname[id]);
fprintf(fp, "%s;\n", lastname[id]);
}
for(id = 5; id < 10; id++){
printf("Record #%d\n", id+1);
fprintf(fp, "%d,", id);
fpfs(fp, "Firstname: ", firstname[id], 1);
fpfs(fp, "Secondname: ", lastname[id], 2);
}
}
void fpfs(FILE *fp, char prom[], char str[], int choice)
{
printf("%s", prom);
scanf("%s", str);
fprintf(fp, "%s", str);
if(choice == 1)
fprintf(fp, ",");
else if(choice == 2)
fprintf(fp, ";\n");
}
void rrecord(FILE *fp)
{
if(fp == NULL){
printf("Error opening file\n");
exit(1);
}
printf("Function is not yet made\n");
}
the above code worked then i decided to put the firstname[][], lastname[][], in the main for making a read file function for the previous records added, and i wanted a pointer to the array for editiing in a function(im crap at explaining but im sure you see what i mean looking at the code)
but now im getting:
file.c: In function `main':
file.c:45: warning: passing arg 2 of `wrecords' from incompatible pointer type
file.c:45: warning: passing arg 3 of `wrecords' from incompatible pointer type
file.c: In function `wrecords':
file.c:89: warning: passing arg 3 of `fpfs' from incompatible pointer type
file.c:90: warning: passing arg 3 of `fpfs' from incompatible pointer type
here the new code that generated the errors above:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
void wrecords(FILE *fp, char *firstname[], char *lastname[], int *id);
void fpfs(FILE *fp, char prom[], char *str[], /*int id, */int choice);
int main(int argc, char *argv[])
{
FILE *fp;
int id = 0;
int recordn[100];
char firstname[100][50];
char lastname[100][50];
int option;
fp = fopen("file.cvs", "a+");
printf("Write and Read File Experment\n\n");
for(;;) {
option = 0;
printf("1: Write Records\n");
printf("2: Read Records\n");
printf("3: Exit Program\n\n");
while(option == 0) {
printf("Please Choose Option: ");
scanf("%d", &option);
if(isdigit(option))
printf("Error Number Wasn't Entered\n");
else if(option < 1 || option > 3)
printf("invalid choice\n");
}
switch(option){
case 1:
wrecords(fp, firstname[id], lastname[id], &id);
break;
case 2:
//rrecord(recordn[1], firstname[1], lastname[1]);
break;
}
if(option == 3)
break;
}
fclose(fp);
printf("File has been closed, end of program\n");
getchar();
}
void wrecords(FILE *fp, char *firstname[], char *lastname[], int *i)
{
int id;
id = *i;
if(fp == NULL){
printf("Error opening file\n");
exit(1);
}
for(id = 0; id < 5; id++) {
printf("Record #%d\n", id+1);
fprintf(fp, "%d,", id);
printf("Firstname: ");
scanf("%s", firstname[id]);
fprintf(fp, "%s,", firstname[id]);
printf("Lastname: ");
scanf("%s", lastname[id]);
fprintf(fp, "%s;\n", lastname[id]);
}
for(id = 5; id < 10; id++){
printf("Record #%d\n", id+1);
fprintf(fp, "%d,", id);
fpfs(fp, "Firstname: ", firstname[id], 1);
fpfs(fp, "Secondname: ", lastname[id], 2);
}
*i = id;
}
void fpfs(FILE *fp, char prom[], char *str[], /*int id, */int choice)
{
printf("%s", prom);
scanf("%s", str);
fprintf(fp, "%s", str);
if(choice == 1)
fprintf(fp, ",");
else if(choice == 2)
fprintf(fp, ";\n");
}
/*
void rrecord()
{
printf("Function is not yet made\n");
}
*/
its started to confuse me ive tried changing stuff also on how i set up the array like i have to i need to use the & when assigning a pointer to the array or does "the name of the array is a pointer to first element in array" rule apply, can any one help?
thanks in advance.