Thread: C file input/out

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    3

    C file input/out

    Hello! I am struggling with the following question, as a beginner, I would like to ask some help if I may

    "Calculate the electric current flowing through a resistor, when given the Resistance and Voltage
    across the resistor. Allow the user to enter 10 sets of data, saving the values of Resistance, Voltage and Current to
    file entitled "Data.txt" (using the equation = V=I*R). Then read the data from the Data.txt and display
    the Power consumed by each resistor."
    here is what I've done so far (doesn't work):

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    int Resistance; int Voltage; int Current; int i;
    
    Voltage = rand() % 100; Resistance = rand() % 50; Current = Voltage / Resistance;
    
    FILE *fp; 
    fp = fopen("data.txt","a+");
    
    if ((fp=fopen("data.txt", "w"))==NULL) {
    printf("Error opening file\n");
    exit(1);
    }
    
    for (i=0;i<10;i++)
    {
    scanf("Enter Voltage & Resistance : %d , %d ", &Voltage, &Resistance);
    fprintf(fp,"%s","%d %d %", &Voltage, &Resistance, &Current);
    }
    
    fclose(fp); 
    getchar();
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You need to explain in more detail what "doesn't work".

    • Does it compile? If not, what error message(s) do you get. Copy-paste them exactly, with line numbers. Note, you should be compiling with warnings turned all the way up, and you should resolve them all, they're usually a sign of doing something wrong.
    • Does it not run or crash? What output does the computer give you when it dies.
    • Does it run but produce no output? Does it produce incorrect output? If incorrect, what input did you give? What output did you get and what output did you expect?


    Also, in the future, please paste your code as plain text, so the forum software can add line numbers and syntax highlighting. Also, make sure your code is properly formatted and indented. This makes it easy to read, and easy for us to help you. It should look like this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
        int Resistance; int Voltage; int Current; int i;
    
    
        Voltage = rand() % 100; Resistance = rand() % 50; Current = Voltage / Resistance;
    
    
        FILE *fp;
        fp = fopen("data.txt","a+");
    
    
        if ((fp=fopen("data.txt", "w"))==NULL) {
            printf("Error opening file\n");
            exit(1);
        }
    
    
        for (i=0;i<10;i++)
        {
            scanf("Enter Voltage & Resistance : %d , %d ", &Voltage, &Resistance);
            fprintf(fp,"%s","%d %d %", &Voltage, &Resistance, &Current);
        }
    
    
        fclose(fp);
        getchar();
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    First, STOP posting in anything except plain text. I can't wade through a sea of formatting junk!


    Quote Originally Posted by Volodya93 View Post
    Hello! I am struggling with the following question, as a beginner, I would like to ask some help if I may

    here is what I've done so far (doesn't work):
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
       int i, I, V,R;    //using V=I*R ( I=V/R )
    
       FILE *fp;=fopen("data.txt","w");
    
       if(!fp) {
          printf("Error opening file\n");
          return 1;
       }
    
       //get user data into the file
       for (i=0;i<10;i++) {
          printf("Enter Voltage & Resistance [V R]: ");
          fflush(stdout);
          scanf("%d  %d", &V, &R);  //use & for numbers, here
          fprintf(fp,"%d %d\n", V, R);  //no &'s here
       }
       fclose(fp);
    
       //now re-open that file for reading - "r" mode, read in the two 
       //values that user gave, for each of the 10 sets of data.
       //and calculate the I value, 
    
    
    }
    Don't add any variables you don't need. Don't change the names of the variables from what the instructions request.

    Simple isn't just good in programming - it's damn near divine.
    Last edited by Adak; 12-03-2012 at 05:07 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > fflush(stdin);
    I thought better of you than to put this in your code.

    > scanf("%d %d ", &V, &R); //use & for numbers, here
    Beware of trailing spaces on scanf formats, they make for interesting typing experiences for users.
    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.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Salem View Post
    > fflush(stdin);
    I thought better of you than to put this in your code.

    > scanf("%d %d ", &V, &R); //use & for numbers, here
    Beware of trailing spaces on scanf formats, they make for interesting typing experiences for users.
    Yep, three A.M. code:

    fflush(stdout); //of course

    and no trailing space, for sure.

    I was able just now to edit them little defects out. (Quite surprised I was under the time limit). Thanks for bringing that up, Salem.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Where do you calculate the current? You should do this for each pair of voltage & current, and write out the three numbers to the file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-13-2012, 10:00 AM
  2. Replies: 2
    Last Post: 03-05-2012, 10:35 AM
  3. linking file stream with input file?
    By flamehead144 in forum C Programming
    Replies: 8
    Last Post: 02-07-2009, 08:55 PM
  4. file input and file pointer problem
    By drater in forum C Programming
    Replies: 6
    Last Post: 02-18-2008, 10:43 AM
  5. Input-Output File--Can't create a file...
    By zaracattle in forum C++ Programming
    Replies: 10
    Last Post: 10-18-2006, 10:15 AM