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.

Recommended Answers

All 2 Replies

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.

@ 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);
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.