0

Hi, I need to open a file containing string values one below the other and display them.
I'm able to open the file but there is some error while displaying them.
Below is my code, kindly guide me to my mistake.

FILE *fp;
char airfoil[6];

fp=fopen("/cygdrive/d/airfoil.txt","r");
if(fp!=NULL)
    {
     while (feof(fp)==0)
      { 
        for(i=0;i<6;i++)
         {
          fscanf(fp,"%s",&airfoil[i]);
         }
      } 
        for(i=0;i<6;i++)
         {
           printf("%s\n",airfoil[i]);
         }
     }/*end of if*/
fclose(fp);

The values in my airfoil text file is:
abc
bcd
cde
def
efg
fgh

P.S: I'm using Netbeans with Cygwin.

3
Contributors
2
Replies
6
Views
6 Years
Discussion Span
Last Post by vinitmittal2008
0

Hi, I need to open a file containing string values one below the other and display them.
I'm able to open the file but there is some error while displaying them.
Below is my code, kindly guide me to my mistake.

Then you made some error programming the code. Your mistake is not telling us what the error is, and what line the error is on.

0

@ phobos666

you have just declared a char array

char airfoil[6];

that means it'll store 6 single char including '\0' char.
you want to store 6 string having 3 char that means you need to declare a string array or can say 2 dimension char array.
your program should be like below assuming string length 4 (including '\0') according to your output:

FILE *fp;
char airfoil[6][4];

fp=fopen("/cygdrive/d/airfoil.txt","r");
if(fp!=NULL)
{
    while (feof(fp)==0)
    { 
        for(i=0;i<6;i++)
        {
             fscanf(fp,"%s",airfoil[i]);
        }
    } 
    for(i=0;i<6;i++)
    {
           printf("%s\n",airfoil[i]);
    }
}/*end of if*/
fclose(fp);
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.