File Input-output

This is a discussion on File Input-output within the C Programming forums, part of the General Programming Boards category; Hello, I need help with file input/output. I need to include statements in my code that will read a file ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    24

    File Input-output

    Hello,
    I need help with file input/output. I need to include statements in my code that will read a file input/output. Thanks for all responses! Here's my code:

    #include <stdio.h>
    #include <math.h>

    int main(void) {

    /* Variable declarations: */
    double SR, CR, L, X, H;

    /*Function body: */
    SR=0.0;
    CR=0.0;
    L= 0.0;
    X= 0.0;
    H= 0.0;
    printf("Enter increment value: ");
    scanf("%lf", &X);
    printf("Enter low value: ");
    scanf("%lf", &L);
    printf("Enter high value:");
    scanf("%lf", &H);

    printf("Value Square Root Cubic Root \n");

    while (L >= H) {
    printf("Low is greater than High, please re-enter:");
    scanf("%lf", &X);
    scanf("%lf", &L);
    scanf("%lf", &H);
    } /* end while */

    while (L <= H) {
    X=1.0/3.0;
    SR = sqrt(L);
    CR = pow(L,X);
    printf("%3.1f %7.5f %7.5f \n",L, SR, CR);

    L = L + 0.1;

    }/* end while */

    return 0;
    } /* end function main */

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    FILE *fp;

    float array[] = {{1}, {2}, {3}, {4}, {5}};

    int i = 0;

    fp = fopen("somefile.txt", "a+");

    if(fp == NULL)
    {return 0;}

    while(!feof(fp) && i < 5)
    {
    fprintf(fp, "%f ",array[i]);
    i++;
    }

    fclose(fp);



    ITSA
    Socket Library!

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    24
    Sebastiani thanks! If you don't mind could you please explain to me your code and where it goes in the program. Am I right to understand that FILE *fp, float array, and int are all Local variable declarations? also, you used FILE *fp, what's the difference in FILE *fp and FILE *in or FILE *out, I am not questioning your code, just trying to understand all that I am reading.

    Again, thanks for everything!

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    24
    Hey Sebastiani how about a reply? Thnx

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Hey Sebastiani how about a reply? Thnx
    We're not personal servents. Not everyone hangs out here all day long just waiting to answer questions.

    Sebastiani thanks! If you don't mind could you please explain to me your code and where it goes in the program. Am I right to understand that FILE *fp, float array, and int are all Local variable declarations? also, you used FILE *fp, what's the difference in FILE *fp and FILE *in or FILE *out, I am not questioning your code, just trying to understand all that I am reading.

    Again, thanks for everything!
    Sure, you could use them as local variable. The only difference between 'FILE *fp' and 'FILE *in' or whatever, is the name. It's just like:

    int x;
    int y;

    Same thing, they're both integers. In this case:

    FILE *fp;
    FILE *in;
    FILE *out;

    They're all file pointers, they're just called different things.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    24
    Hi Quzah

    Thanks for your reply, I didn't mean to offend you or anyone else. I honestly was not referrinf to anyone as personal servents, I am not here everyday either.

    Question: Did my post really upset you or you was just being mean? Maybe I was just being DUMB but I saw that my post was leaving the top 25 thread and Sebastiani probably would not have seen it had it left so I posted a please reply again, sorry if i seemed rude but it was not my intentions, it wont happen again!

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No. I seldom take offense. I just get irritated when people immediately expect a response. For example, a few days ago, some one posted in reply to their own thread about four times asking why they hadn't got a reply and getting angry when they weren't answered.

    I just figure most people forget the fact that this is just a board of people who help in their free time, and they demand immediate response.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 08:31 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. File Input and Output, simple.
    By Vber in forum C Programming
    Replies: 5
    Last Post: 11-17-2002, 01:57 PM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM

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