vendredi 9 juin 2017

I scan a .dat file with an array and print it but when i reprint it it prints out weird numbers

i asked the user for a file with an array and i copy the array from the file to the c[50] array, also printing out each value as i add the values to the array. Out of curiosity i decided to reprint the values off of the c[50] array and to my surprise the were totally different numbers (BIG numbers). PLS HELP!! I'm dying

#include <iostream>
#include <stdio.h>
using namespace std;

//Bubble Sort 
void bubble_sort (int arr[], int n)
 {
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n - i - 1; ++j)
      if (arr[j] > arr[j + 1])
     {
        int temp = arr[j];
        arr[j] = arr[j + 1];
        arr[j + 1] = temp;
      }
  } 
 int main()
{
    int c[50], k=0,d[50],i,s;
    float r ;
 FILE *fin;
 char filename[15],l;
 printf("De el nombre del archivo:\n\n");
 scanf("%s", filename);
fin=fopen(filename, "r");
 if(fin == NULL)
 { printf("\nNo se pudo abrir el archivo.\n");
 return 1;}
 fscanf(fin, "%c",&c[k]);
 while(!feof(fin))
 { printf("%c" , c[k]);
 k++;
 fscanf(fin, "%c",&c[k]);
 } 
 //s is the size of the array idk why it makes the k double the amount that what it really is but it doubles it.
 s=k/2;
 printf("%d\n",s);


 for (i=0;i<s;i++)
 {printf("%d  ",c[i]);
 }

return 0;
}




Aucun commentaire:

Enregistrer un commentaire