Thread: please help me with this files program

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    113

    please help me with this files program

    Please give me code for the following :
    The program should take one file as input which contains integers and should take it into a int variable. lets say, print that variable on the screen.

    I wrote the following code but its showing errors..
    Code:
    # include <stdio.h>
    
    int main (int argc, int argv[1] )
    {
    FILE *file_num ;
    int num ;
    
    file_num = fopen (argv[1],"r");
      if (!file_num) 
      printf ("File not found\n");
    fscanf (file_num,"%d",&num);
    printf("%d",num);
    return ;
    }
    The programming is functioning when I replace these int types with char type but I need integers not characters.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    What error?
    Check argc > 1.
    check fscanf() return value.

    main should be either
    Code:
    int main(void)
    int main(int ,char * [ ]);
    main 2nd argument is array of pointer to char.
    You are using 2nd argument of your main function as file name.
    myprogram test.txt

    argv[0] = "myprogram"
    argv[1] = "test.txt"
    Last edited by Bayint Naung; 05-11-2011 at 04:38 AM.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    This is what I am getting at terminal when I execute the code.
    I even removed 1 from main (int argc, int argv[1], still this is what I 'm getting
    test.c: In function ‘main’:
    test.c:10: warning: passing argument 1 of ‘fopen’ makes pointer from integer without a cast
    /usr/include/stdio.h:249: note: expected ‘const char * __restrict__’ but argument is of type ‘int’

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Hello...
    Code:
    int main (int argc, int argv[1] )
    Try...
    Code:
    int main (int argc, char *argv[])

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I even removed 1 from main (int argc, int argv[1], still this is what I 'm getting
    But you didn't change the int to char did you?

    Read your C book again to see how main should be declared.
    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.

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    Yes i did changes even that @salem

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Then post your latest code and latest error message(s), if you're still stuck.
    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.

  8. #8
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    @salem
    Code:
    test.c: In function ‘main’:
    test.c:10: warning: passing argument 1 of ‘fopen’ makes pointer from integer without a cast
    /usr/include/stdio.h:249: note: expected ‘const char * __restrict__’ but argument is of type ‘char’

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Then post your latest code and latest error message(s), if you're still stuck.
    If this is too hard for you, try something other than programming.
    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.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I'm just curious to see how many times he has to be told it's ... char *argv[] ... before he actually changes it.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I'm betting that the fopen call is now something like
    Code:
    fopen( argv[1][0], "r");
    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.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salem View Post
    I'm betting that the fopen call is now something like
    Code:
    fopen( argv[1][0], "r");
    Code:
    fopen (*&argv*[1][0],"%r++");

  13. #13
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    Dear Programmers!
    I'm very sorry, first I didn't go through all your posts clearly. so, I didn't catch what you are saying. ..
    I have changed to what Mr. Salem has said char *argv[].

    @ Salem, @ CommonTater : Thanks for your great comments
    Code:
    fopen( argv[1][0], "r");
    fopen (*&argv*[1][0],"%r++");

  14. #14
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    Its working!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Get program to copy itself into program files, then start on startup.
    By guitarist809 in forum Windows Programming
    Replies: 6
    Last Post: 03-03-2008, 09:42 AM
  3. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  4. How To Make The Program Installed In Program Files Folder?
    By javacvb in forum Windows Programming
    Replies: 4
    Last Post: 11-05-2003, 05:33 PM
  5. My first C++ program with files
    By vasanth in forum C++ Programming
    Replies: 1
    Last Post: 04-11-2002, 10:23 AM

Tags for this Thread