I am getting some build code errors I do not understand how to fix. This is probably because I am so new to programming. Help is appreciated! Line 54 error is missing semicolon before type and lines 56,59, and 60 say i is undeclared???

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

//Global Variables
char array[10][20];
FILE *inf,*ouf;
int n;

//Function prototype
int ReadFile();
void Sort();
void WriteFile();

//Main Function
int main()
{
    /*Reading user input*/
    char input[20],output[20];
    printf("Enter input file name:");
    scanf("%s",input);
    inf = fopen(input,"r");
    /*Check for input file*/
    if(inf==NULL)
    {
        printf("Input file not exists");
        return 1;
    }
    
    printf("Enter output file name:");
    scanf("%s",output);
    ouf = fopen(output,"w");
    /*Check for output file*/
    if(ouf==NULL)
    {
        printf("Cannot create output file");
        return 1;
    }
    /*Call functions*/
    n = ReadFile();
    Sort();
    WriteFile();    

    /*Close files*/
    fclose(inf);
    fclose(ouf);

    return 0;
}

/*Function to read data from file*/
int ReadFile()
{
     printf("Reading from file");
     int i=0;
     //Read and store in array
     while(fscanf(inf,"%s",array[i])!=EOF)
     {
     /*Display data*/
     printf("%d - %s\n",i,array[i]);                                           
     i++;
     };
     return 1;
}

void Sort()
{    int i,j;
     /*Bubble Sorting*/
     for(i=0;i<n-1;i++)
     {
         for(j=0;j<n-i;j++)
         {
                 if(strcmp(array[j],array[j+1])>0)
                 {
                 /*Swapping*/
                 char *temp;
                 strcpy(temp,array[j]);
                 strcpy(array[j],array[j+1]);
                 strcpy(array[j+1],temp);
                 }
         }
     }
}
/*Function to write to the file*/
void WriteFile()
{
     int i;
     printf("Writing to file");
     for(i=0;i<=n;i++)
     {
     /*Display data*/
       printf("%s\n",array[i]);
       fprintf(ouf,"%s\n",array[i]);
     }
     
}

Variables must be declared at the top of the block. Move your declaration above the printf statement.
Also, you have an extra semicolon on line 61(EDIT: Though that seems to pass through ok, I guess it's optional).

Edited 6 Years Ago by jonsca: n/a

There are two lines 22 and 32 that have this warning:22) : warning C4047: '=' : 'FILE *' differs in levels of indirection from 'errno_t' and a warning and an error for line 22 that say: warning C4024: 'fopen_s' : different types for formal and actual parameter 1 and 22) : error C2198: 'fopen_s' : too few arguments for call. Can someone help me understand how I coded incorrectly on these lines please?

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


//Global Variables
char array[10][20];
FILE *inf,*ouf;
int n;

//Function prototype
int ReadFile();
void Sort();
void WriteFile();

//Main Function
int main()
{
    /*Reading user input*/
    char input[20],output[20];
    printf("Enter input file name:");
    scanf_s("%s",input);
    inf = fopen_s(input,"r");
    /*Check for input file*/
    if(inf==NULL)
    {
        printf("Input file not exists");
        return 1;
    }
    
    printf("Enter output file name:");
    scanf_s("%s",output);
    ouf = fopen_s(output,"w");
    /*Check for output file*/
    if(ouf==NULL)
    {
        printf("Cannot create output file");
        return 1;
    }
    /*Call functions*/
    n = ReadFile();
    Sort();
    WriteFile();    

    /*Close files*/
    fclose(inf);
    fclose(ouf);

    return 0;
}

/*Function to read data from file*/
int ReadFile()
{
     int i=0; 
	printf("Reading from file");
         /*Read and store in array*/
     while(fscanf(inf,"%s",array[i])!=EOF)
     {
     /*Display data*/
     printf("%d - %s\n",i,array[i]);                                           
     i++;
     };
     return 1;
}

void Sort()
{    int i,j;
     /*Bubble Sorting*/
     for(i=0;i<n-1;i++)
     {
         for(j=0;j<n-i;j++)
         {
                 if(strcmp(array[j],array[j+1])>0)
                 {
                 /*Swapping*/
                 char *temp;
                 strcpy(temp,array[j]);
                 strcpy(array[j],array[j+1]);
                 strcpy(array[j+1],temp);
                 }
         }
     }
}
/*Function to write to the file*/
void WriteFile()
{
     int i;
     printf("Writing to file");
     for(i=0;i<=n;i++)
     {
     /*Display data*/
       printf("%s\n",array[i]);
       fprintf(ouf,"%s\n",array[i]);
     }
     
}

I have one warning at line 77 that says temp is not initialized. Please show me where to fix this. Thanks!

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


//Global Variables
char array[20][20];
FILE *inf,*ouf;
int n;

//Function prototype
int ReadFile();
void Sort();
void WriteFile();

//Main Function
int main()
{
    /*Reading user input*/
    char input[10],output[20];
    printf("Enter input file name:");
    scanf_s("%s",input);
   fopen_s(&inf,input,"r");
    /*Check for input file*/
    if(inf==NULL)
    {
        printf("Input file does not exist");
        return 1;
    }
    
    printf("Enter output file name:");
    scanf_s("%s",output);
   fopen_s(&ouf, output,"w");
    /*Check for output file*/
    if(ouf==NULL)
    {
        printf("Cannot create output file");
        return 1;
    }
    /*Call functions*/
    n = ReadFile();
    Sort();
    WriteFile();    

    /*Close files*/
    fclose(inf);
    fclose(ouf);

    return 0;
}

/*Function to read data from file*/
int ReadFile()
{
     int i=0; 
	printf("Reading from file");
         /*Read and store in array*/
     while(fscanf_s(inf,"%s",array[i])!=EOF)
     {
     /*Display data*/
     printf("%d - %s\n",i,array[i]);                                           
     i++;
     };
     return 1;
}

void Sort()
{    int i,j;
     	     /*Bubble Sorting*/
     for(i=0;i<n-1;i++)
     {
         for(j=0;j<n-i;j++)
         {
                 if(strcmp(array[j],array[j+1])>0)
                 {
                 /*Swapping*/
                 char *temp;
		 strcpy_s(temp,12,array[j]);
                 strcpy_s(array[j],12,array[j+1]);
                 strcpy_s(array[j+1],12,temp);
                 }
         }
     }
}
/*Function to write to the file*/
void WriteFile()
{
     int i;
	 printf("Writing to file");
     for(i=0;i<=n;i++)
     {
     /*Display data*/
       printf("%s\n",array[i]);
       fprintf(ouf,"%s\n",array[i]);
     }
     
}

Move char* temp to the top of the function sort. Right after the line 67
and make it

char* temp=0;

Its a good programming practice to initialize all variables to 0. Helps to reduce errors

I think you are just 'moving too fast'. Study the documentation, for example fopen_s().

Actually, for all the *_s functions -- stop using them. They are not standard which makes it very difficult to help you, and will make it difficult to write code if you need to use a different compiler.

And post the EXACT error messages, not a paraphrase. It helps us to see exactly what the compiler says.

Actually, for all the *_s functions -- stop using them.

I would rephrase that a little

Actually, for all the *_s functions -- stop using them if you aim to write standards-compliant code. Otherwise, learn and use them (these functions exist for a good reason). Just be aware that these functions are available only with recent Microsoft's compilers.

I have one warning at line 77 that says temp is not initialized. Please show me where to fix this.

About swapping, you need to have a buffer for swapping the strings. A mere char * used as you intended is not enough (it would cause havoc if used). So, at the very least, change to

/*Swapping*/
  /* 'temp' - a buffer for swapping the strings */
  char temp[12];
  strcpy_s(temp,12,array[j]);
  strcpy_s(array[j],12,array[j+1]);
  strcpy_s(array[j+1],12,temp);

You might use a #define to define the sizes of the char arrays, so that it would be consistent throughout the code. As of now there are buffers of sizes 10, 12, 20.

So maybe

#define STRING_BUFFER_SIZE 20
int main()
{
    char input[STRING_BUFFER_SIZE], output[STRING_BUFFER_SIZE];
    /* ... and so on */
This article has been dead for over six months. Start a new discussion instead.