Converting a program from C++ to C

This is a discussion on Converting a program from C++ to C within the C Programming forums, part of the General Programming Boards category; So, theres this program which works for C++, but im having problems when i try to convert it for C, ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    5

    Converting a program from C++ to C

    So, theres this program which works for C++, but im having problems when i try to convert it for C, below is the program with the changes that i made to try to make it work for C, and the way it was before is shown with ''//....". When i try to compile it i get the message "cannot convert 'const char*' to 'FILE*' for argument '1' to 'int fscanf(FILE*, const char*,...)' " at line 24, which is "fscanf("%d", &M[j][k]);" what should i do??

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    unsigned int N[6][3]=
    {
        1, 0, 2,
        1, 2, 0,
        0, 1, 2,
        0, 2, 1,
        2, 1, 0,
        2, 0, 1
    };
    
    int main(void)
    {
        unsigned int M[3][3], j=0, k=0, p=0, menor=~0, soma=0, i=0;
        FILE *arq;          //fstream arq("garrafas.eco",ios::in);
        arq = fopen("garrafas.eco", "r");
        
        while(!feof(arq))// while(!arq.eof())
        {
           for(j=0; j<3; j++)
              for(k=0; k<3; k++)
                 fscanf("%d", &M[j][k]);          //arq >> M[j][k];    
           for(i=0; i<6; i++)
           {     
              for(j=0; j<3; j++)
                 for(k=0; k<3; k++)
                    if(k!=N[i][j]) soma+=M[j][k];      
              if(soma<=menor)
              {
                 menor=soma;      
                 p=i;             
              }
           }       
           for(j=0; j<3; j++)
           printf("%c", N[p][j]);            //cout << "MVB"[N[p][j]];
           printf("%d", menor);            //cout << " " << menor << endl;
        }
        system("PAUSE");
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,749
    You forgot to provide arq as an argument to fscanf. By the way, using feof to control a loop like that risks looping one more time than you intend because the eof condition is only set on a failed read.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    5
    Quote Originally Posted by laserlight View Post
    You forgot to provide arq as an argument to fscanf. By the way, using feof to control a loop like that risks looping one more time than you intend because the eof condition is only set on a failed read.
    hey thanks for the reply but how could i do it? how do i provide arq as an argument to fscanf? sorry im just new to programmimg and i had lots of help to make this program. And wont feof finish the loop when it reaches the end of the file that is being read? the file that will be read is supposed to have 9 numbers in it and that's it.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    You can read about feof here.

    Here is how you can use EOF

    Hmm, how would I find out about the fscanf function?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 09-29-2010, 12:18 PM
  2. Help converting array program to link list program
    By hsmith1976 in forum C++ Programming
    Replies: 0
    Last Post: 02-14-2010, 08:50 PM
  3. Converting a program. From Vector to List
    By Swerve in forum C++ Programming
    Replies: 5
    Last Post: 12-05-2009, 03:48 PM
  4. Problem with time converting program.
    By brian75 in forum C++ Programming
    Replies: 2
    Last Post: 11-24-2009, 04:38 PM
  5. Why is this program converting to integers
    By 3DPhreak in forum C++ Programming
    Replies: 3
    Last Post: 05-31-2004, 09:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21