Excellerant
Technical User
Below is the code I have been using. The puts command does display the right information. When I try to copy that information into my array it occupies into all parts of the array not just the current index position. As the while loop continues it then writes over most of the data in each. Not sure what else I could do to make this work. Any help would be greatly appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100
#define CurrencyCount 6
char Currencies[6];
static char Titles [MAX];
int length;
int main(int argc, char *argv[])
{
int ch; // place to store each character as read
FILE *fCurrency; // "file pointer
long count = 0;
char Values [MAX];
int index =0;
char c;
//open currency file
if ((fCurrency = fopen("currency.txt", "r")) == NULL)
{
printf("Cant open %s\n", fCurrency);
exit(1);
}
rewind(fCurrency);
while (fgets(Titles,MAX,fCurrency) != NULL && Titles[0] != '\n' && index < CurrencyCount)
{
puts(Titles);
length = strlen(Titles)+1;
strncpy( &Currencies[index], Titles, length);
printf("This is Currency: %s",&Currencies[index]);
printf("This is index: %d\n\n",index);
index++;
}
for(index=0;index<CurrencyCount;index++)
printf("Finally, index %d is currency %s\n",index,&Currencies[index]);
getchar();
//getchar();
if (fclose(fCurrency) != 0)
fprintf(stderr,"Error closing file\n");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100
#define CurrencyCount 6
char Currencies[6];
static char Titles [MAX];
int length;
int main(int argc, char *argv[])
{
int ch; // place to store each character as read
FILE *fCurrency; // "file pointer
long count = 0;
char Values [MAX];
int index =0;
char c;
//open currency file
if ((fCurrency = fopen("currency.txt", "r")) == NULL)
{
printf("Cant open %s\n", fCurrency);
exit(1);
}
rewind(fCurrency);
while (fgets(Titles,MAX,fCurrency) != NULL && Titles[0] != '\n' && index < CurrencyCount)
{
puts(Titles);
length = strlen(Titles)+1;
strncpy( &Currencies[index], Titles, length);
printf("This is Currency: %s",&Currencies[index]);
printf("This is index: %d\n\n",index);
index++;
}
for(index=0;index<CurrencyCount;index++)
printf("Finally, index %d is currency %s\n",index,&Currencies[index]);
getchar();
//getchar();
if (fclose(fCurrency) != 0)
fprintf(stderr,"Error closing file\n");
return 0;
}