Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

copy text file data into an array 1

Status
Not open for further replies.

Excellerant

Technical User
May 23, 2003
16
US
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;
}
 
man which array???
anyways this sounds like your incrementing where u dont need to
use fstream to read file
then from buffer copy into array or whatever
 
char Currencies[6][MAX];

and Currencies[index] instead of &Currencies[index]
 
What mingis said. You need a 2-dimensional array... learn more about arrays before you proceed further in the language.
 
Thank you all. Unfortuantly my internet was down again yesterday so I could not try this.

sethmcdoogle, I am not sure why you suggest a 2-dimensional array though. I am bringing a list of six Currency names. The intention was to bring in the list of names from a text file, assign them to an array and close the text file. Then use the array within the program. With some minor differences, this code worked for bringing in the conversion rates.

Thanks for responding, I will attempt again to access this from home this evening and post an update ASAP.

Excellerant

 
I am not sure why you suggest a 2-dimensional array though

Why then you use one-dimensional array for single name?
It is because char in C is one byte long integer and strings are null-terminated arrays of char.
 
Thank you all for your assistance. I was not understanding that although I created the text file from a 1-dimensional array it would require a 2-dimensional array to retrieve that same data. That was all it took.

Your help is greatly appreciated.

Excellerant

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top