Member Avatar for CanofCornInfini

Hello, I'm new to C and I'm not sure what is wrong with my code here.
After I execute, I get this:

Debug assertion failed!
[program address]
File: fgets.c
Line: 60
Expression: str != NULL

It's a really simple program. I'm trying to assign family data from the in file to the out file with formatting (things commented out), but this is just the start of that program but I can't finish it out since I ran into this problem.
I searched the forum first but this same error is either unsolved in C, or in C++. Or the situation is different because the code is more complicated.
Anyway, here's my full code:

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



void main (void)
{
 FILE *in, *out;
 char people[81];
 int number;
 
 
 
 printf("This program reads in from familyin.txt, formats its data,\n and writes it out to familyout.txt.");
 in = fopen ("c: \\familyin.txt", "r");
 out = fopen ("c:\\familyout.txt", "w");

 fgets (people, 80, in);
 number = atoi(people);
 printf("The text from familyin.txt is \n %s \n", people);
 printf("The numberic value is %i. \n\n", number);
 fprintf(out, "The text from familyin.txt is \n %s \n", people);

 /*familyin = fopen (
 // struct family      {
//				      char name [50];			/* person's name */
//					  char street [50]; 		/* street address */ 
//				      char csz [50];			/* city, state, zip */
//				      char relation [30];		/* relation to you */
////				      char birthday [11];		/* mm-dd-yyyy */
//			         };
 // struct family people[7];

 fclose (in);
 fclose (out);
 
}

Recommended Answers

All 5 Replies

You can not open the same file twice. Use just one FILE* pointer and open it for both reading and writing if you need to.

@Dragon I think he is opening two different files ..

One is familyin.txt and familyout.txt..
Whr you talking about those two???

commented: Yes, you are right :) +34

I run your program, didnt got any error ..

On 16th line while opening file you gave extra space between C: and \\

c: \\familyin.txt"

i just removed that space and program worked fine..

Member Avatar for CanofCornInfini

Shankye,
Haha, that did it. I was getting so frustrated. Thanks all!

Thr are some so called Good programming practices in C, Try to follow them to avoid frustration ..

While opening file, use something like this..

if( (file = fopen("FileName.txt",r)) == NULL )
   {
         printf("Error opening file \n Program exiting\n");
         system("pause");
         exit(0);
   }

If file couldnt be opened in that case NULL is returned by fopen() function,
so by checking whether its NULL or not we will be sure about file operation ..

Happy programming ..

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.