Like Tree1Likes

What is wrong with that easy program ?

This is a discussion on What is wrong with that easy program ? within the C Programming forums, part of the General Programming Boards category; Hello, I would like to know where is my mistake on that program I made : Code: #include <stdio.h> int ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    29

    What is wrong with that easy program ?

    Hello,

    I would like to know where is my mistake on that program I made :

    Code:
     #include <stdio.h>
    
    int main (int argc, char*argv[]) {
    
        if (argc > 1) {
            printf ("there are %d elements",argc); 
            printf ("the first element is %c",argv[1]);}
        else 
            if {printf("there is one element");}
    FILE*f1;
    f1 = fopen(argv[1], "w");
    fprintf(f1,"%c",argv[2]);
    fclose(f1);
    return 0;}
    when I try to compile it it says :

    Code:
    test.c:7:3: warning: format ‘%c’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat]
    test.c:12:1: warning: format ‘%c’ expects argument of type ‘int’, but argument 3 has type ‘char *’ [-Wformat]

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,771
    Oop, my prior links were not the warning your were getting

    read dmh2000 answer below.

    Tim S.
    Last edited by stahta01; 06-05-2012 at 10:49 AM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    argv[n] is a char *, not a char. if you want to print the whole argument, use argv[n] and %s. if you want to print only the first character of the argument, use argv[n][0] and %c

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    29
    oh right I am stupid... thanks a lot

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    29
    Thank you, this tutorial will still be usefull. (stahta01)

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    29
    %s , s if for string right ?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Yes.
    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

  8. #8
    Registered User
    Join Date
    Apr 2012
    Posts
    29
    Well so I tryed to modify my program so that I create a file with all of the arguments and not only the first one, and once again it doesn't work. can you help me again pease ?

    Code:
    #include <stdio.h>
    
    int main (int argc, char*argv[]) {
    int i=0;
        if (argc > 1) {
            printf ("there are %d elements",argc); 
            printf ("the first element is %s",argv[1]);}
        else 
            {printf("there is one element");}
    FILE*f1;
    f1 = fopen(argv[1], "w+");
    for (i=1;i<=argc;i++){
    fprintf(f1,"%s",argv[i]);
    fclose(f1);}
    return 0;}

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    What is your current code?
    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

  10. #10
    Registered User
    Join Date
    Apr 2012
    Posts
    29
    sorry I modified my post.

  11. #11
    Registered User
    Join Date
    Apr 2012
    Posts
    29
    oh I think I know

  12. #12
    Registered User
    Join Date
    Apr 2012
    Posts
    29
    So I changed it to that :
    Code:
    #include <stdio.h>
    
    int main (int argc, char*argv[]) {
    int i=0;
        if (argc > 1) {
            printf ("there are %d elements",argc); 
            printf ("the first element is %s",argv[1]);}
        else 
            {printf("there is one element");}
    FILE*f1;
    f1 = fopen(argv[1], "w+");
    for (i=1;i<=argc;i++){
    fprintf(f1,"%s",argv[i]);}
    fclose(f1);
    return 0;}
    now I have a segmentation falt
    Last edited by dekl; 06-06-2012 at 10:55 AM.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    For starters, you need to indent your code:
    Code:
    #include <stdio.h>
     
    int main(int argc, char *argv[]) {
        int i = 0;
        if (argc > 1) {
            printf("there are %d elements", argc);
            printf("the first element is %s", argv[1]);
        } else {
            printf("there is one element");
        }
        FILE *f1;
        f1 = fopen(argv[1], "w+");
        for (i = 1; i <= argc; i++) {
            fprintf(f1,"%s", argv[i]);
            fclose(f1);
        }
        return 0;
    }
    Now it becomes obvious that you are calling fclose prematurely.
    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

  14. #14
    Registered User
    Join Date
    Apr 2012
    Posts
    29
    Code:
    #include <stdio.h>
    
      
    int main(int argc, char *argv[]) {
        int i = 0;
        if (argc > 1) {
            printf("there are %d elements", argc);
            printf("the first element is %s", argv[1]);
        } else {
            printf("there is one element");
        }
        FILE *f1;
        f1 = fopen(argv[1], "w+");
        for (i = 1; i <= argc; i++) {
            fprintf(f1,"%s", argv[i]);
            }
        fclose(f1);
        
        return 0;
    }
    thanks, I need to work on that. Now this has a segmentation falt.

  15. #15
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    argv[argc] does not exist, yet you are printing it at the end of your loop.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 09-06-2009, 02:48 PM
  2. Help me ! easy c program
    By DINORS in forum C Programming
    Replies: 21
    Last Post: 06-27-2008, 11:59 PM
  3. Help with very easy program
    By plshelpme in forum C Programming
    Replies: 9
    Last Post: 11-02-2005, 12:11 PM
  4. Tell me a easy program to make PLEASE!! PLEASE PLEASE!!!!
    By oobootsy1 in forum C++ Programming
    Replies: 49
    Last Post: 08-12-2003, 12:30 AM
  5. Easy Program.....won't compile Help!!!!!!!!!!!!
    By incognito in forum C++ Programming
    Replies: 2
    Last Post: 11-29-2001, 08:08 PM

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