Thread: basic file operations queries

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

    Question basic file operations queries

    I'm C Programming learner.
    Please do clear my queries in detail.
    < My PC config : Linux with GNU GCC compiler >

    1) How to take a file as input which contains integers and display on screen.
    I used the following code but its not working :
    Code:
    # include <stdio.h>
    int main ( int argc, int *argv[1] )
    {
    FILE *fp_file;
    int num;
    
    fp_file = fopen ( argv[1], "r" );
    fscanf  (fp_file,"%d",&num);
    fprintf  (stdout,"%d",num);
    
    fclose (fp_file);
    return ;
    }
    This is what I used and I'm getting errors. Instead If I replace 'int' by 'char' @ argv[] , @ num and replace %d by %c, its working..
    but here the issue is that I'm getting only the first letter of the characters in the file on the screen. Even though I didn't give any white spaces.
    I then replaced fscanf() and fprintf() functions with fputc and fgetc but the output is same.

    how do I make sure that a) I get the whole characters without using arrays concept. b) how do I get numbers on my screen.
    c) can't I use int type in declaring arguments in main function.

    2) I want to take input a file which contain some number and my program should read and take that number into a declared variable. how to do it.

    * also please give me tips how troubleshoot myself..

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Open the data file in notepad and paste it into code tags here... we need to see what's in it.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Well, remember that although the data *looks* like integers, they are really characters (it's an ASCII file, right?)

    You should always test fp_file to see if fopen worked.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mike65535 View Post
    Well, remember that although the data *looks* like integers, they are really characters (it's an ASCII file, right?)
    That's why I want to see the file... fscanf() should take care of the conversion for him...

    You should always test fp_file to see if fopen worked.
    Yep... and he should test the return value of fscanf() to make sure the conversion worked before printing.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Quote Originally Posted by CommonTater View Post
    fscanf() should take care of the conversion for him...
    Ah, right.

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

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    I mean, the pointer stream fp_file

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    also please give me tips how troubleshoot myself..
    Best advice I can offer is to write a little code, test a lot. This means you generate compartmentalized code, a piece at a time. You'll never code yourself into a corner if you test along the way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Performing File operations using File Inode number
    By rak1986 in forum C Programming
    Replies: 4
    Last Post: 09-22-2008, 02:43 AM
  2. Help with File operations
    By shwethu in forum C Programming
    Replies: 5
    Last Post: 04-29-2008, 05:52 AM
  3. Timing basic operations in C++
    By StevenGarcia in forum C++ Programming
    Replies: 9
    Last Post: 09-18-2007, 02:10 AM
  4. text file queries
    By ozzy34 in forum C++ Programming
    Replies: 2
    Last Post: 09-30-2004, 10:42 AM
  5. Hash File Queries
    By 1999grandamse in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2002, 02:18 PM

Tags for this Thread