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.
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
if i do
fgets(file_name);
it sends me an error.
could you give me an example?
Refer to my post #12 for an example.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
gets is inherently vulerable to buffer overflow, so that is unacceptable. What did you try for fgets and how does it not work?Originally Posted by ashley1nonly
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.Originally Posted by ashley1nonly
Yes.Originally Posted by ashley1nonly
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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; }
Refer to my example from post #12. Notice that I called fgets like this:Originally Posted by ashley1nonly
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:if (fgets(text, sizeof(text), fp))
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?Code:fgets(file_name);
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.,Originally Posted by ashley1nonly
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 */
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
if(fgets(file_name, sizeof(100), fp))
for anyone who might need help later heres my final code that i got
bacon.txt was a file that i made and save numbers on itCode:#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; } }