6 Replies - 2518 Views - Last Post: 20 February 2010 - 05:40 PM Rate Topic: -----

#1 newcuser  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 04-February 10

Code Build Errors

Posted 20 February 2010 - 02:19 PM

HI Everyone,
I am getting some build errors when I run this program that I do not know how to fix. 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. Your assistance in correcting my code is appreciated!The idea is to have a user type the name of an input text file and an output text file.
#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]);
     }
     
}


Is This A Good Question/Topic? 0
  • +

Replies To: Code Build Errors

#2 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: Code Build Errors

Posted 20 February 2010 - 02:22 PM

You need to check the number of parameters that fopen_s requires. Details may be found here.

Hope that helps.
Was This Post Helpful? 1
  • +
  • -

#3 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Code Build Errors

Posted 20 February 2010 - 02:29 PM

The first argument for fopen_s should be the FILE * ptr:
fopen_s(&inf, input, "r");


and
fopen_s(&ouf, output, "w");


shoould do the trck.
Was This Post Helpful? 1
  • +
  • -

#4 newcuser  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 04-February 10

Re: Code Build Errors

Posted 20 February 2010 - 03:21 PM

Thank you Martyn Rae and Sarmanu,
I had caught the drift that I needed that extra argument but forgot the ampersand when I put it in so it still did not compile correctly. That left me wondering what to do next.



View Postsarmanu, on 20 February 2010 - 01:29 PM, said:

The first argument for fopen_s should be the FILE * ptr:
fopen_s(&inf, input, "r");


and
fopen_s(&ouf, output, "w");


shoould do the trck.

Was This Post Helpful? 0
  • +
  • -

#5 newcuser  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 04-February 10

Re: Code Build Errors

Posted 20 February 2010 - 03:58 PM

After having changed the code I have two primary issues I need to resolve but do not understand how to resolve. I now get this error message for lines 22 and 32:warning C4047: '=' : 'FILE *' differs in levels of indirection from 'errno_t',,,and I am not sure what I have done in the swapping portion of the bubble sort but I am getting multiple errors. I know that strcpy lines are not written quite right...I need at least one other argument...beyond that I am stuck. Please help
#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);
    inf = 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);
    ouf = 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,array[j]);
                 strcpy_s(array[j],array[j+1]);
                 strcpy_s(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]);
     }
     
}


View Postsarmanu, on 20 February 2010 - 01:29 PM, said:

The first argument for fopen_s should be the FILE * ptr:
fopen_s(&inf, input, "r");


and
fopen_s(&ouf, output, "w");


shoould do the trck.

Was This Post Helpful? 0
  • +
  • -

#6 newcuser  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 04-February 10

Re: Code Build Errors

Posted 20 February 2010 - 04:35 PM

All errors fixed...One warning left that says temp is an uninitialized variable. Please show me where to initialize it.



View Postnewcuser, on 20 February 2010 - 01:19 PM, said:

HI Everyone,
I am getting some build errors when I run this program that I do not know how to fix. 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. Your assistance in correcting my code is appreciated!The idea is to have a user type the name of an input text file and an output text file.
#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]);
     }
     
}

Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,219
  • Joined: 23-August 08

Re: Code Build Errors

Posted 20 February 2010 - 05:40 PM

This should cause your program to crash.
/*Swapping*/
char *temp;
strcpy(temp,array[j]);
strcpy(array[j],array[j+1]);
strcpy(array[j+1],temp);


temp there is a pointer to a location in memory...however, it's pointing who knows where in memory. So when you strcpy your data into that pointer, you could be overwriting something else in your program.

You need to allocate memory by either using a stack-allocated buffer
char temp[20];

or by dynamically allocating memory through malloc
char *temp = malloc(strlen(array[j] + 1);
and then freeing when you're done with it
free(temp);


I would suggest in this simple circumstance to use the buffer.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1