1

I want to store the data in location but whenever I run the code it give me message that the fp is undeclared. I want fp to be working in another function. How to do this?

#include <stdio.h>
#include <stdlib.h>
#define MAX 15

int new_acc(char *name, size_t namesize);
int list_view(char *name);
int main(){
    FILE *fp;
    fp = fopen("/home/Documents/file.txt","w");      /* this is fp */

    int one='1', two='2';
    char choice[MAX], name[15] = "";
    do{
        printf("%c. Create new account\n",one);
        printf("Enter your choice: ");
        fgets(choice, sizeof choice, stdin);

        if (choice[0] == one)
            {new_acc(name, sizeof name);}
    }
    while(two != choice[0]);
    return 0;}

int new_acc(char *name, size_t namesize){
    printf("Enter your name: ");
    fgets(name, namesize, stdin);
    fputs(name, fp);
    fclose(fp);

    return 0;}

2 Answers2

3
int new_acc(char *name, size_t namesize)

Change to:

int new_acc(FILE * fp, char *name, size_t namesize)

Then when you call this function in main:

new_acc(fp, name, sizeof name);

You should check the return value of fopen:

fp = fopen("/home/Documents/file.txt","w"); 
if(!fp) {
  // handle the error.
}

You should move fclose(fp) out of this function.

OT, you should change the type:

int one='1', two='2';

to char one='1', two='2'; because you compare these variables with char value.

Updated for the question in comment:

#include <stdio.h>
#include <stdlib.h>
#define MAX 15

int new_acc(FILE *fp, char *name, size_t namesize);

int main(){
    FILE *fp;
    fp = fopen("file.txt","w");      /* this is fp */
    if(!fp) {
        printf("cannot open the file");
        return -1;
    }

    char one='1', two='2';
    char choice[MAX], name[15] = "";
    char all_accouts[100][15];
    int i = 0;
    do{
        printf("%c. Create new account\n",one);
        printf("Enter your choice: ");
        fgets(choice, sizeof choice, stdin);

        if (choice[0] == one)
            {new_acc(fp, all_accouts[i], sizeof(all_accouts[i])); i++;}
    }
    while(two != choice[0]);

    for(int j = 0; j < i; j++) {
        printf("%s\n", all_accouts[j]);
    }
    fclose(fp);
    return 0;

}

int new_acc(FILE *fp, char *name, size_t namesize) {
    printf("Enter your name: ");
    fgets(name, namesize, stdin);
    fputs(name, fp);
    return 0;
}

output of test:

1. Create new account                                                                                                     
Enter your choice: 1                                                                                                      
Enter your name: name1                                                                                                    
1. Create new account                                                                                                     
Enter your choice: 1                                                                                                      
Enter your name: name2                                                                                                    
1. Create new account                                                                                                     
Enter your choice: 2                                                                                                      
name1                                                                                                                     

name2                                                                                                                     
Hitokiri
  • 3,607
  • 1
  • 9
  • 29
  • 2
    And move the `fclose(fp)` out of `new_acc` – Robert Navado May 08 '20 at 19:19
  • 1
    oh, i did not see fclose in this function. I edited the answer. Thanks – Hitokiri May 08 '20 at 19:25
  • Here is problem that I am facing is how to save that data, because whenever I start the previous result just finish itself from text file. I want to save that to use that another time. – Natasha Bibi May 08 '20 at 20:07
  • you mean, you want to save the name, each time you create new account, right ? You can use the a 2D array: `char all_accounts[100][15]` (it means you can store maximum 100 accounts). Then use the counter, `i` for example, when you call the function`new_acc`: `new_acc(fp, all_accounts[i], sizeof all_accounts[i]);`. after creating one name you increase the counter `i++` – Hitokiri May 08 '20 at 20:15
0
  1. Change the following line

    int new_acc(char *name, size_t namesize);

to

int new_acc(char *name, size_t namesize, FILE *fp);

As the file pointer will be handy - BTW - Perhaps chose a better name for the variable

  1. Change

    {new_acc(name, sizeof name);}

to

{new_acc(name, sizeof name, fp);}

As this will be required

  1. Change

    int new_acc(char *name, size_t namesize){

To

  int new_acc(char *name, size_t namesize, FILE *fp){

to match above and fp is required in the function

Job done!

Ed Heal
  • 59,252
  • 17
  • 87
  • 127