0
#include <stdio.h>
#include <math.h>


FILE *input, *output;

main()
{

int Max, Min, x[1000000],i=1,n=0,j=0;

input = fopen ("Nube_de_Caminantes.txt","r");

n=getw(input);

while (n !=EOF)
{
      fscanf(input, "%i",x[i]);
      i++;
      n = getw(input);
}



fclose(input);

}

There is some stuff I need to do with the array once I have it, but I can't get this to work. It compiles fine but it crashes if I try to run it. I'm quite the newbie when it comes to C, any help is welcome. Thank you.

4
Contributors
5
Replies
7
Views
6 Years
Discussion Span
Last Post by TigrisAltaica
0

No error message, just crashes? Nothing to give you an indication why? That's really strange. Try to pinpoint what statement causes the problem.

0
#include <stdio.h>
#include <math.h>


FILE *input, *output;

main()
{

int Max, Min, x[100000],i=1,n=0,j=0;

input = fopen ("Nube_de_Caminantes.txt","R");
output = fopen ("Histograma.txt","W");
n=getw(input);

while (n !=EOF)
{
      fscanf(input, "%i",x[i]);
      i++;
      n = getw(input);
}


fclose(input);
for (i=1; i<=10; i++) fprintf(output,"%i\n", x[i]);
fclose(output);
}

Ok, by changing the size of the array and changing the "w" and "r" to capital letters, it no longer crashes. So I proceeded to alter the code to write some numbers into an output file to check if I'm reading the data correctly, however, now it does nothing. Thanks.

0

probably good idea to check return codes for errors. For example if you are not able to open the file...

0

> changing the size of the array

This is right. In your original code you were trying to allocate 4 megabytes of data on the stack. The default stack size is 1 meg.

0

Okay, now the only thing that doesn't work is the file-reading bit, all the values for the array come back as zero. Any suggestions? Thank you! Quick edit:

I played around with it, and realized it will crash only if the input file contains more that two numbers. All the output values come out blank regardless.

Edited by TigrisAltaica: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.