I have a small problem why is this code not writting information into the userdata.txt file?
I am not getting any errors in code blocks it runs but the file does not get any data.
While in visual c++ the code gives me "debug assertion failed...expression: str(!=NUll)".
What am i doing wrong ?

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>


FILE *spdata;                                         //stream created(globaly)
int day, year; char month[16];                        //Variables created(globaly)

int open_file(FILE *spdata);                          //creates a file function
void user_input( int, char[], int);                   //user input fucntion
int write_file(int , char[], int);                    //writes user input to file

int main()
{

   open_file(spdata);                           
   user_input(day,month,year);
   write_file(day,month,year);

   fclose(spdata);                                    // file closed
   getch();
}

int open_file(FILE *spdata)
{
    spdata = fopen("userdate.txt","w");               //name of file created
    if(spdata != NULL)                                // if file test is passed
    printf("\nFile created");

    else
    printf("\nFile was not created");                //if file test failed

    return 0;
}

void user_input( int day, char month[16], int year)
{
      printf("\n\nEnter day: ");
      scanf("%d",&day);

      printf("Enter month: ");
      scanf("%s", month);

      printf("Enter year: ");
      scanf("%d",&year);


}

int write_file(int day , char month[16], int year)
{
    fprintf(spdata,"%d %s %d", day, month,year);        //data written to file
    printf("File has been written");

    return 0;
}

Recommended Answers

All 6 Replies

The problem is with the argument passing methods. Before the discussing the solution to this issue, we shall see the concepts a bit.

There are two types of passing arguments to the function.

  • Pass by Value
  • Pass by Reference

In the pass by value, only the values are copied to the calling function argument variables. For example

foo(int b)
{

}

main()
{
    a=10;
    foo(a);
}

In this case, the variable b which is the argument of function foo will get the copy of the value in a ie 10. So the changes done on variable b won't get reflected upon a.

But, in the pass by reference case, the sample code will be as follows

foo(int *b)
{

}

main()
{
    a=10;
    foo(&a);
}

Here, the address of a **is passed to the argument **b. So, the changes done on b will get refleted on a becuase we are changing the value present in the address of a. (Point to Note)

SOLUTION:

Having read this, now here comes the solution to the program. (I compiled in linux gcc -- removed the conio.h and getch(), they are not supported in the gcc package. )

Whenever, you are using the global variables, there is no necessary to pass the variables as argument. Anyways, that is a different story.

The parameters to the function user_input are pass by value for day and year (The month is a array, which means the address will be passed by default), so the values which are assigned in the function won't get reflected when the function call return to main function. Use pass by reference instead.

Next thing is the spdata file pointer. When a pointer variable is to be passed as argument in the call by reference method, then it is necessitated to pass it as an double pointer.

So the code snipped will be

int open_file(FILE **spdata); 

and during invocation

open_file(&spdata);

Following is the working code after doing the above said modification.
NOTE: I have removed the #include <conio.h> and getch().

#include <stdio.h>
#include <stdlib.h>
FILE *spdata; //stream created(globaly)
int day, year; char month[16]; //Variables created(globaly)
int open_file(FILE **spdata); //creates a file function
void user_input( int *, char[], int *); //user input fucntion
int write_file(int , char[], int); //writes user input to file
int main()
{
    open_file(&spdata);
    user_input(&day,month,&year);
    write_file(day,month,year);
    fclose(spdata); // file closed
}
int open_file(FILE **spdata)
{
    *spdata = fopen("userdate.txt","w"); //name of file created
    if(spdata != NULL) // if file test is passed
        printf("\nFile created");
    else
        printf("\nFile was not created"); //if file test failed
    return 0;
}
void user_input( int *day, char month[16], int *year)
{
    printf("\n\nEnter day: ");
    scanf("%d",day);
    printf("Enter month: ");
    scanf("%s", month);
    printf("Enter year: ");
    scanf("%d",year);
}
int write_file(int day , char month[16], int year)
{
    fprintf(spdata,"%d %s %d", day, month, year); //data written to file
    //fprintf(spdata,"%d %s %d", day, month,year); //data written to file
    printf("File has been written");
    return 0;
}

thank you...i noticed that when at times the text file gets the data of the array but the day and year values are blank...again thank you very much

...

i have a next problem is someone could help me i cant display the data written to this file in a switch statement what am i doing wrong

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>


FILE *fp;                     //creates a file pointer

typedef struct date_info
{
  int day;
  char month[12];
  int year;
} DATE;


DATE get_date(void);                                       //fucntion to input date info

int open_file(FILE **fp);                                  //function to open a file
void write_file(DATE );                                    //writes data to file
void get_file (DATE  );                                    //Gets data within file
int read_file(FILE **fp);                                  //Reads information contained in main file


int menu();


int main()
{
   DATE date;                                              //Structure data type and variable declared
   int input;
   input = menu();                                       //Menu displayed and choice recived

   switch(input)
   {
   case 1:
      system("cls");

      printf("\n Enter the date please !! ");
      open_file(&fp);

      date = get_date();

      write_file(date);

      printf("\n File Written");
      fclose(fp);
   break;

   case 2:

       read_file(&fp);
       get_file(date);

       printf("\n\n");
       printf("\n -----------------------------------------");
       printf("\n generated:%d-%s-%d",date.day,date.month,date.year);
       printf("\n -----------------------------------------");
   break;

   case 3:
       system("cls");
       printf("\nPress [Enter] To exit the program ");
       getch();
       return 0;
   break;

   default :
       printf("\nError wrong input bye !! ");

   break;
  }//End of CASE

getch();
}

/*FUNCTION DEFINITIONS */

int menu()
{
     int choice;

     printf("\n Welcome ");
     printf("\n -------------------------------");

     printf("\n Press  [1] to Enter the date");
     printf("\n Press  [2] to Display the date");
     printf("\n Press  [3] to Exit");
     printf("\n\n Enter Enter your choice : ");
     scanf("%d",&choice);

     return choice;
}


DATE get_date(void)
{
    DATE a;

    printf("\n Enter the day :");
    scanf("%d",&a.day);

    printf(" Enter the month:");
    scanf("%s", a.month);

    printf(" Enter the year :");
    scanf("%d",&a.year);

    return a;
}

int open_file(FILE **fp)
{
    *fp    = fopen("Test.txt","w");
    return 0;
}


int read_file(FILE **fp)
{
    *fp  = fopen("Test.txt","r");
    return 0;
}


void write_file(DATE a )
{
    fprintf(fp ,"  %d %s %d",a.day,a.month,a.year);
}

void get_file (DATE a )
{
    fscanf(fp ,"%d %s %d",&a.day,a.month,&a.year);
}

This is an exact copy of another request I answered on another board - so I'd change it around to avoid suspicion of copying.

First, in get_file(DATE a). Change it from a void function, so it will return the DATE a, to the calling function:

DATE get_file(DATE a)  

//and the new last line:
return a;

Change the prototype of the function up near the top of the file, also, to match this.

Now you need to have a DATE struct to "catch" the DATE struct being returned. That should be done on line #52, above:

date=get_file(date);

The data was being read by the function get_file(), but it wasn't being returned.

....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.