Thread: Help with program

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    22

    Help with program

    I'm working on a program for school, and its supposed to accept a command line argument to print to a file, but it has to be functional both ways. I cant quite figure out where I'm going wrong it just prints the error statements. Can anyone give me any hints/ideas?
    Thanks
    Code:
    /*Assignment: #1 part #2
        Puropse: Prints ten lines using two nested loops.
        
    */
    
    #include <stdio.h>
    
    
    main(int argc, char *argv[])
    {
    
    /*    FILE *fopen(int argc, char *argv[i]);*/
        FILE *fp;
        int a = 0;
        int b = 0;
        int i;
        
        if (argc < 2)
        {
            fp = fopen(argv[1], "rw+");
            if (!fp)
                printf("Can't open file \n"); 
                else if(argv[1] != '\0')
                {
                    while(a < 10)
                    {
                        a = 0;
                        ++b;
                        fprint("\n");
                        while(a < b)
                        {
                            fprint("\n");
                            ++a;
                        }
                    } 
                }                          
                else if(argv[1] == '\0')
                {
                    while(a < 10)
                    {                    
                        a = 0;
                        ++b;
                        printf("\n"); 
                        while(a < b)
                        {
                            printf("&#37;d", a);                                   
                           ++a;           
                        }
                    }
               }
        }
        
        else (printf("Too many arguments"));
        
        printf("\n");
      
        
    }
    Last edited by Brimak86; 10-20-2007 at 07:48 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,056
    Using perror() would tell you why the file could not be opened for example.
    Code:
            fp = fopen(argv[1], "rw+");
            if (!fp) {
                perror("Can't open file");            
            }
    Always use { } to mark the scope of the code to be executed. If you add another statement and forget them, your program will still compile but it won't work as expected.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Misspelling printf as fprint?

    [edit]Or the fact that when you enter command line parameter(s), argc oughtta be at least 2? Perhaps...?
    Code:
    if (argc >= 2)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    22
    ok so I worked a little more on this, now I'm getting errors saying line 36 and 39 fprintf is not a function. I want to print the stuff to an external data file, I thought it was fprintf in C, any help is appericated.
    Thanks

    Code:
    /*  Assignment: #2 part #1
        Puropse: Prints ten lines using two nested loops, and accept comand line 
        arguments to print the output to an external data file.
        
    */
    
    #include <stdio.h>
    
    
    
    main(int argc, char *argv[2])
    {
    
    /*    FILE *fopen(int argc, char *argv[2]); */
        FILE *fprintf; 
        fprintf = fopen(argv[1], "rw+");
        int a = 0;
        int b = 0;
        int i;
        
        if (argc <= 2)
        {
            fprintf = fopen(argv[1], "rw+");
            if (!fprintf)
            {
                perror("Can't open file \n"); 
            }
                else if(argv[1] != '\0')
                {
                    while(a < 10)
                    {
                        a = 0;
                        ++b;
                        fprintf("\n");
                        while(a < b)
                        {
                            fprintf("&#37;d", a);
                            ++a;
                        }
                    } 
                }                          
                else if(argv[1] == '\0')
                {
                    while(a < 10)
                    {                    
                        a = 0;
                        ++b;
                        printf("\n"); 
                        while(a < b)
                        {
                            printf("%d", a);                                   
                           ++a;           
                        }
                    }
               }
        }
        
        else (printf("Too many arguments"));
        
        printf("\n");
        
    fclose(fprintf);
        
    }

  5. #5
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    First off, I wouldn't call your FILE* fprintf. I'm not completely sure if it does anything bad at all, but it's just asking for trouble. Name it something like fp or something.

    Then, fprintf, unlike printf, takes 2 or more parameters. The first one is the FILE* to which you want to print, the rest are like any typical printf statement.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Happy_Reaper View Post
    First off, I wouldn't call your FILE* fprintf. I'm not completely sure if it does anything bad at all, but it's just asking for trouble. Name it something like fp or something.

    Then, fprintf, unlike printf, takes 2 or more parameters. The first one is the FILE* to which you want to print, the rest are like any typical printf statement.
    It is perfectly valid to call a variable the same as a function - as long as you don't use the variable and the function in the same scope - which the above code attempts to do. That will lead to "not a function" error messages.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    It is perfectly valid to call a variable the same as a function - as long as you don't use the variable and the function in the same scope - which the above code attempts to do. That will lead to "not a function" error messages.
    Right, that's what I was thinking.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM