Hi, I have a text file which looks like this:

3Com 3C996B Gigabit Server NIC
NEC PCI to USB Enhanced Host Controller (B1)
....
....

Now, I need a 'C' Program which will copy each line into an array of strings or character array, such as char* Cards[10] . Could any body pls help me out?

Thanks in advance,
- Vijay

use FILE and associated functions in stdio.h. First call fopen() to open the file, then in a loop call fgets() to read each line. After end-of-file is reaches call fclose() to close the file.

If you use an array of char pointers as you posted you will want to call malloc() to allocate memory for the pointer before copying the line into the array.

I have the following sample.

int main()
{
FILE * pFile;
char string [100];

// char filename = "c:\\cards.txt";
pFile = fopen ("c:\\cards.txt" , "r");
if (pFile == NULL) perror ("Error opening file");
else {
while(fgets (string , 100 , pFile))
printf("%s",string);
fclose (pFile);
}
return 0;
}

The above program displays the following output.

3Com 3C996B Gigabit Server NIC
NEC PCI to USB Enhanced Host Controller (B1)

But, I want to read those lines into an array of strings, which size is vary depends on the no.of lines. pl suggest me how to achieve this.

Thanks in advance,
- Vijay

that's a pretty good start. have you learned linked lists yet? if you have, then use a linked list to store the strings. If not, then just use a simple array of pointers as in your previous post. Just make an array large enough, or you can use malloc() and realloc() to dynamically increate the array size as needed while reading the file.

I did it in the following way.

int main()
{
FILE * pFile;
char string [10][100];
int i=0,n=0;
 
 
pFile = fopen ("c:\\cards.txt" , "r");
if (pFile == NULL) perror ("Error opening file");
else {
while(fgets (string[i] , 100 , pFile))
{
printf("%s",string[i]);
i++;
}
 
fclose (pFile);
}
i=n;
for(i=0;i<n;i++)
printf("%s",string[i]);
return 0;
}

But instead of setting up a fixed length array, pls suggest me the steps to create a dynamic array using malloc(), or realloc().

If you know how to use pointers then you can do this:

// declare initial 2d array;
char **array = 0;
// number of strings it will hold
int   arraySize = 0;

// allocate a block of 10 pointers which will store 10 strings
arraySize = 10;
array = realloc(array, arraySize * sizeof(char *));

// store a string in the 0th array element
array[0] = strdup("Hello World");

You will put the last few lines inside the loop that reads the file. Use another integer to count the number of lines that have been read. When that counter is the same as arraySize in my example above then you will have to expand the array to accommodate more strings. Do that by increasing the value of arraySize by some value, say 10.

Hi, I have used it with fixed size array, and the code is below.

int main()
{
FILE * pFile;
char cards[10][100];
int i=0,n=0;
pFile = fopen ("c:\\cardnames.txt" , "r");
if (pFile == NULL) perror ("Error opening file");
else 
{
while(fgets (cards[i] , 100 , pFile))
i++;
fclose (pFile);
}
n=i;
for(i=0;i<n;i++)
printf("%s",cards[i]);
return 0;
}

But, I want to make it in c++, rather native C, & I want to declare the array as wchar_t cards[10][100]. But, I think fgets only returns char*. so, how can I convert it to wchar_t. or is there any alternate function to fgets, which returns the current line, and the return type as wchar_t

This article has been dead for over six months. Start a new discussion instead.