Thread: Read from a file and find slope, midpoint, and distance

  1. #31
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current code?

    By the way, if no one has posted after you, consider editing your post instead of posting a new one, unless you are no longer able to edit as the edit time window has ended.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #32
    Registered User
    Join Date
    Feb 2015
    Posts
    83
    it has a mx of 100 numbers to be entered and it will open the file with the numbers and display them now finally lol. But now i need to be able to set the values to x1 y1 x2 y2 so i can find the slope midpoint and distance.

    I need the program to read all of the values then display them. then find the points and slope....
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
       char ch, file_name[100];
       FILE *fp;
    
       printf("Enter the name of file you wish to see\n");
       gets(file_name);
    
       fp = fopen(file_name,"r"); // read mode
    
       if( fp == NULL )
       {
          printf("Error while opening the file.\n");
    
       }
    
       printf("The contents of %s file are :\n", file_name);
    
       while( ( ch = fgetc(fp) ) != EOF )
          printf("%c",ch);
    
       fclose(fp);
       return 0;
    }
    Last edited by ashley1nonly; 02-26-2015 at 01:33 AM.

  3. #33
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You could consider using fscanf to parse the numbers in the file.

    As was mentioned earlier, you should not use gets. I have demonstrated usage of fgets.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #34
    Registered User
    Join Date
    Feb 2015
    Posts
    83
    if i do
    fgets(file_name);

    it sends me an error.

    could you give me an example?

  5. #35
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Refer to my post #12 for an example.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #36
    Registered User
    Join Date
    Feb 2015
    Posts
    83
    it still didnt work so ill just keep the way i have it setup. How exactly would you get the numbers to fit into the coordinates. Im thinking doing a while loop until all the values are plugged in

    i know i need to create a place to store the values like int x1, x2, y1, y2;

    if i can get this part then i will be able to use switch statements to find the slope midpoint and distance
    Last edited by ashley1nonly; 02-26-2015 at 02:01 AM.

  7. #37
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ashley1nonly
    it still didnt work so ill just keep the way i have it setup.
    gets is inherently vulerable to buffer overflow, so that is unacceptable. What did you try for fgets and how does it not work?

    Quote Originally Posted by ashley1nonly
    How exactly would you get the numbers to fit into the coordinates. Im thinking doing a while loop until all the values are plugged in
    You know in advance that there are four values for the coordinates, right? If so, you do not need a loop: you could even just use one fscanf call with a format string that has four format specifiers.

    Quote Originally Posted by ashley1nonly
    i know i need to create a place to store the values like int x1, x2, y1, y2;
    Yes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #38
    Registered User
    Join Date
    Feb 2015
    Posts
    83
    this is what i tried for fgets, and no there could be anywhere between 0-100 points given. if we are given over 100 our programs arent supposed to read them. just say some points were left out
    Code:
    #include <stdio.h>
    #include <stdlib.h>
          
    int main()
    {
       char ch, file_name[100];
       FILE *fp;
    
       printf("Enter the name of file you wish to see\n");
       fgets(file_name);// here i plugged in my f
    
       fp = fopen(file_name,"r"); // read mode
    
       if( fp == NULL )
       {
          printf("Error while opening the file.\n");
    
       }
    
       printf("The contents of %s file are :\n", file_name);
    
       while( ( ch = fgetc(fp) ) != EOF )
          printf("%c",ch);
    
       fclose(fp);
       return 0;
    }

  9. #39
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ashley1nonly
    this is what i tried for fgets
    Refer to my example from post #12. Notice that I called fgets like this:
    Code:
    if (fgets(text, sizeof(text), fp))
    The first argument is a pointer to the first element of the char array where the result is to be stored. The second argument is the number of elements of that char array, including space for the null character. The third argument is the FILE pointer. Contrast this to your code:
    Code:
    fgets(file_name);
    It is exceedingly obvious that your call of fgets is clearly and absolutely wrong. I can understand if you got the arguments mixed up, or made an off by one error, or did not check the return value, or something like that, but did you not read the code that I wrote in post #12, including the line on which I called fgets, despite me having directed it to you twice?

    Quote Originally Posted by ashley1nonly
    no there could be anywhere between 0-100 points given. if we are given over 100 our programs arent supposed to read them. just say some points were left out
    In that case you should have an array of 100 elements to store these points and use a loop to read the numbers. Check the return value of fscanf to determine when to end the loop, e.g.,
    Code:
    for (i = 0; i < 100 && fscanf(fp, "%d", &numbers[i]) == 1; ++i)
    {
        /* this could be left empty, or you do so extra validation on numbers[i], etc */
    }
    /* at this point, i contains the number of elements in use */
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #40
    Registered User
    Join Date
    Feb 2015
    Posts
    83
    if(fgets(file_name, sizeof(100), fp))

  11. #41
    Registered User
    Join Date
    Feb 2015
    Posts
    83
    for anyone who might need help later heres my final code that i got

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    int main(void)// starts the program
    {
       FILE *fp;// the pointer // the file pointer
       float  x1,y1, x2, y2;// floats my points
       float slope;// floats the slope
       float midp;// floats the midpoint
       float midp2;// floats the second midpoint value
       float dist;// floats the distance
    
       fp = fopen("bacon.txt","r"); // read the file "r"
    
       if( fp == NULL )
    
          printf("File could not be opened\n");//  file will not open
    
    
    else
    {
    printf(" Slope      Midpoint\t Distance\n");// stores the heading for my slope midpoint and distance
    
       while (!feof(fp))// starts the while loop
        {
          fscanf(fp,"%f%f%f%f",&x1,&y1,&x2,&y2);// scans and stores the values
    
            if((x2-x1)==0)// looking for undefined slope
            {
            printf("undefined");
            }
            else
     {
            slope=(y2-y1)/(x2-x1); // slope equation
            printf("%.1f      ", slope);
            }
            midp=(x1+x2)/2;// midpoint for x equation
            midp2=(y1+y2)/2;// midpoint for y equation
            printf("  (%.1f, %.1f)", midp, midp2);// prints out the midpoints
            dist=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));// distance formula
            printf(" \t  %.1f\n", dist);// prints out the distance
    }
    
       fclose(fp);// closes the file
       return 0;
          
    
            
    }
    }
    bacon.txt was a file that i made and save numbers on it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. midpoint programming
    By Merve Güler in forum C Programming
    Replies: 2
    Last Post: 03-28-2013, 01:04 PM
  2. How to find segmental time and distance
    By adr in forum Tech Board
    Replies: 2
    Last Post: 11-09-2006, 09:28 AM
  3. Read string until find char
    By 3saul in forum C Programming
    Replies: 10
    Last Post: 06-20-2006, 08:24 AM
  4. Read a txt and find data
    By BianConiglio in forum C Programming
    Replies: 8
    Last Post: 04-25-2004, 03:14 AM
  5. Slope Formula
    By Dangerous Dave in forum C Programming
    Replies: 9
    Last Post: 10-08-2001, 12:25 PM