Thread: reading a number from a file

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    17

    reading a number from a file

    Q(1);
    This is my code for reading in a number and storing it in a file.
    but when i go to print out the number from the file it wont work.does anyone know why.

    Q(2);
    when the number is stored in a file it is stored as a string. How do i convert the string into a int value.



    Code:
    #include<stdio.h
    void main()
    {
    FILE *fp;
    char num[2];
    char number[2];

    printf("Enter Number");
    scanf("%s",&number);
    fp=fopen("file.txt","r+");

    if(fp==NULL)
    puts("Error in opening file.txt");
    else
    {
    puts("file.txt is opened");
    fputs(number,fp);
    fgets(num,2,fp);
    printf("%s",num);
    fclose(fp);
    }
    }
    When i find myself in times of trouble. mother mary comes to me, speaking words of wisdom, let it be C

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164

    Re: reading a number from a file

    Originally posted by the_head
    Q(1);
    This is my code for reading in a number and storing it in a file.
    but when i go to print out the number from the file it wont work.does anyone know why.

    Q(2);
    when the number is stored in a file it is stored as a string. How do i convert the string into a int value.


    Use the code tags around the entire program
    Code:
    #include<stdio.h
    void main()
    {
    FILE *fp;
    char num[2];
    char number[2];

    printf("Enter Number");
    scanf("%s",&number);
    fp=fopen("file.txt","r+");

    if(fp==NULL)
    puts("Error in opening file.txt");
    else
    {
    puts("file.txt is opened");
    fputs(number,fp);
    fgets(num,2,fp);
    printf("%s",num);
    fclose(fp);
    }
    }
    Q1) you cannot write then read the file. After writing, close then open the file for reading.

    Also, when inputting data into a string, use fgets() instead of scanf(), you'll have fewer problems with your input.

    Q2) in the file or after reading as a string?
    If in the file, convert to an integer using atoi() before writing then output using fwrite(). Your file is now a binary file, not text.

    If after reading the string from the file, use atoi() and you have an int.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    you can just use
    Code:
    fscanf(in_file,"%d",&myInt);
    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM