0

So I want to use a for loop to read in 10 different numbers into a text file. The problem with my program is that it crashes while inputting the 4th number. How do I fix this? Here is my code:

#include <stdio.h>
#include <stdlib.h>  /* For exit() function */
int main()
{
   int x[20], i;
   FILE *fptr;
   fptr=fopen("line.txt","w");

   printf("Enter numbers\n");
   for (i=0; i<10; i++){
        gets(x[i]);
   }
   fprintf(fptr,"%d", x);
   fclose(fptr);
   return 0;
}
Martin James
  • 24,453
  • 3
  • 36
  • 60
  • 1
    Don't use `gets()` ever. But, it happens to be inappropriate to use on an `int` array. – jxh Apr 21 '16 at 04:01
  • 1
    Did you read the [gets manual](http://linux.die.net/man/3/gets) to see what it does and what the type of its argument is? And did you take any notice of the warnings given by your compiler? – kaylum Apr 21 '16 at 04:01
  • 1
    Does it still crash after changing gets to scanf? The biggest problem I see is the attempty to fprintf x (an array of int) to the file as if it were just an int. What you probably want is to loop through all 10 elements of x again and fprintf each one individually. – Christopher Oicles Apr 21 '16 at 04:21
  • You're including `stdlib.h` 'for `exit()`', but I don't see you using `exit()` anywhere. If you don't end up using it, you can remove that `"include` directive and make your program a bit smaller. In `main()`, you can use `return` to achieve the same results as `exit()`. Calling `return 1;` from `main()` will achieve the same results as calling `exit(1):`. It is useful for exiting the program within a function however. – RastaJedi Apr 21 '16 at 04:27
  • Please do not edit the question so as to make answers irrelevant/incorrect. If you have somthig to add to your code, either APPEND the new version or ask another question. – Martin James Apr 21 '16 at 07:07

4 Answers4

1

You need to use scanf, don't use gets. gets is not safe to use in this manner, it reads a string, not a number. I woudl avoid gets anyway. Have a look at this answer on how to read integers....

How to read numbers separated by space using scanf

Community
  • 1
  • 1
Harry
  • 11,298
  • 1
  • 29
  • 43
1

You should use scanf

scanf("%d", x[i])

gets give your a string instead of an interger.

Pan Ruochen
  • 1,990
  • 6
  • 23
  • 34
1

This is a problem though, after you edited with scanf, note fprintf(fptr,"%d", x); will not work as you expected, x is not an integer, it's an array of integers, so you need write each of the array into the file, you can use a loop to do this:

for (i=0; i<10; i++) {
    fprintf(fptr,"%d", x[i]);
}
fclose(fptr);
fluter
  • 13,238
  • 8
  • 62
  • 100
1
  • In this, You are taking value one by one into array using for loop but can you observed that how you are printing array into file?
  • You also need to for loop to print all the 10 value in file.
int main()
{
  int x[20], i;
  FILE *fptr; 
  fptr = fopen("line.txt","w");
  printf("Enter numbers\n");

    for (i = 0; i < 10; i++)
    {
      gets(x[i]);
    }

   for(i = 0; i < 10 ;i++)
   {
     fprintf(fptr,"%d", x[i]);
   }
  fclose(fptr);
  return 0;
}
  • Also you can do like this :
 int main()
{
  int x[20], i;
  FILE *fptr; 
  fptr = fopen("line.txt","w");
  printf("Enter numbers\n");

    for (i = 0; i < 10; i++)
    {
      gets(x[i]);
      fprintf(fptr,"%d", x[i]);
    }
  fclose(fptr);
  return 0;
}