0

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

Edited by Nick Evan: Fixed tags. Snippet -&gt; thread

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by GrubSchumi
0

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

0

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++;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.