hi....i am writing a code which reads the contents of file....it has first name, lastname and phone number.....now i want ot read all the first names into one array second into another and phone numbers into third array....i tried but i could read only line by line.....please help out...my code is here
thanx in advance

#include <stdio.h>
#include <string.h>


int main()
{
 char c[50];
 FILE *file;


 file = fopen("in.txt", "r");

 if(file==NULL) {
   printf("Error: can't open file.\n");

   return 1;
 }
 else
{
   printf("File opened successfully. Contents:\n\n");
}

   while(fgets(c, 50, file)!=NULL)
   {

    printf("String: %s", c);


}

   printf("\n\nNow closing file...\n");
   fclose(file);
   return 0;
 }

but i need an out put like this...
array1=list of first names
array2=list of last names
array3=list of phone numbers

Recommended Answers

All 2 Replies

if the first name, last name, phone number are space-separated, then read this to learn how to parse the line

Hi,

If the first name second name and phone number are consistently in this order throughout the file then an ifstream read object in a while loop may work;

read>>firstnamearray[n]>>lastname[n]>>phonenumberarray[n];
n++;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.