Reputation: 808
I am trying to add integers to an array in C. However I'm reading characters in from a file, so I have to convert them to integers first. For some reason my program wont even start up now before it stops working. I think it is a conversion problem but I am new to C.
new edit code:
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
FILE* fp;
const char filename[] = "test.txt";
char ch;
int num[1000];
int j = 0;
char temp;
fp = fopen(filename, "r");
if( fp == NULL )
{
printf( "Cannot open file: %s\n", filename);
exit(8);
}
while(!feof(fp))
{
temp = fgetc(fp);
num[j] = temp - '0';
j++;
}
printf("First in the array is: %d, last is; %d", num[0], num[999]);
fclose(fp);
return EXIT_SUCCESS;
}
test.txt
731671765313306249192251196744265747423553491949349698352031277450632623957831801698480186947885184385861560789112949495459501737958331952853208805511
125406987471585238630507156932909632952274430435576689664895044524452316173185640309871112172238311362229893423380308135336276614282806444486645238749
303589072962904915604407723907138105158593079608667017242712188399879790879227492190169972088809377665727333001053367881220235421809751254540594752243
525849077116705560136048395864467063244157221553975369781797784617406495514929086256932197846862248283972241375657056057490261407972968652414535100474
821663704844031998900088952434506585412275886668811642717147992444292823086346567481391912316282458617866458359124566529476545682848912883142607690042
242190226710556263211111093705442175069416589604080719840385096245544436298123098787992724428490918884580156166097919133875499200524063689912560717606
0588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450
Someone to point me in the right direction would be great.
Upvotes: 0
Views: 402
Reputation: 28535
I am rephrasing my entire answer after you added the contents of test.txt
.
Instead of
while(!feof(fp))
{
temp = fgetc(fp);
num[j] = temp - '0';
j++;
}
Change to
while(!feof(fp)&& (j<1000))
{
temp = fgetc(fp);
if ( temp != EOF && ( temp>='0' && temp<='9') ) // To filter out non-numbers and also the last EOF
num[j++] = temp - '0';
}
And instead of
printf("First in the array is: %d, last is; %d", num[0], num[999]);
Change to
printf("First in the array is: %d, last is; %d", num[0], num[j-1]); // j-1 so that it works even when there are less than 1000 numbers and to keep it generic
And your done!
Upvotes: 1
Reputation: 5334
The following application works and gives the following output (in a textfile containing 1 3 5 5565)
int main (void)
{
FILE* fp;
const char filename[10] = "test.txt";
char ch;
int count = 0;
int num[100];
fp = fopen(filename, "r");
if( fp == NULL )
{
printf( "Cannot open file: %s\n", filename);
exit(8);
}
while (fscanf(fp, "%d", &num[count]) !=EOF){
count++;
}
rewind(fp);
printf("Text file contains: %d\n", count);
printf("First in the array is: %d, last is; %d\n", num[0], num[count-1]);
fclose(fp);
system("pause");
return EXIT_SUCCESS;
}
if you want the array to be dynamicly sized, you need to loop thru the elements and count them, before adding them, just like you did in your example in the main code. And instead of while (fscanf(fp, "%d", &num[count]) !=EOF){
you use a temp counter to index the elements
Hope this helps
Upvotes: 0
Reputation:
You cannot dynamically allocate an int array in C like this, so
int num[count]
will not work. Use a maximum value or dynamic allocation using malloc.
Also you cannot pass a single char to atoi, you have to pass a 2-element char array with the second element being 0, or you could just use
num[j] = temp - '0';
Also checking fp for EOF in the second while loop is wrong, use feof(fp)
or temp != EOF
Hope this helps you a bit to get it working.
Upvotes: 1