Frage

My program will run and display info correctly but as soon as I press any key to continue I get program has stopped working screen, I don't know why, here is my code

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

int main(int argc, char *argv[])
{
  char A[50][30];
  printf("Hello \n");

  FILE *file;

  file = fopen("Strings.dat","r");



  printf("The contents of the array are :\n");

  int ch = 0;

  while(fgets(A[ch], 30, file))
  {
    int length = strlen(A[ch]);
    /*
  if((A[ch][length-1]) == '\n')
  {
    A[ch][length-1] = NULL;
  }
*/
  ch++;
   }
  fclose(file);
  printf("%s",A[0]);
  system("PAUSE");
}

Can anyone explain what I did wrong?

if you're wondering the commented code is commented out because it gave me a warning "[Warning] assignment makes integer from pointer without a cast [enabled by default]" I thought taking it out would fix my problem

War es hilfreich?

Lösung

Try adding exit(0); after your pause statement. This will tell your program to exit completely. The 0 is simply a return value. You can return whatever you want, but 0 is standard for "no errors" or something like that.

EDIT: On second thought, simply add return 0; at the end. You may not even need the exit(0) statement (it depends on how you're running your program).

SECOND EDIT: The exit() statement simply force-closes your application. There is still an underlying issue. Give me a bit and I will try to track it down for you.

Hope this helps!

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top