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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    83

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

    Its constantly saying that my code will not open will i have numbers in my file. I am used note pad and i saved my file as bacon.txt
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main (void)
    {
    float x1;
    float x2;
    float y1;
    float y2;
    FILE*Fp;// file pointer
    if((Fp=fopen("bacon.txt", "r"))==NULL)
    {
    puts("File would not open");
    }
    else
    {
    printf("%f%f%f%f\n", "x1", "y1", "x2","y2");
    fscanf(Fp,"%f%f%f%f", &x1,&y1,&x2,&y2);
    }
    }

  2. #2
    Registered User
    Join Date
    Feb 2015
    Posts
    83
    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);
    
       fp = fopen(file_name,"r"); // read mode
    
       if( fp == NULL )
       {
          perror("Error while opening the file.\n");
          exit(EXIT_FAILURE);
       }
    
       printf("The contents of %s file are :\n", file_name);
    
       while( ( ch = fgetc(fp) ) != EOF )
          printf("%c",ch);
    
       fclose(fp);
       return 0;
    }
    still want open my file
    Last edited by ashley1nonly; 02-25-2015 at 10:24 PM.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    opens fine for me, BUT does have a warning about int main,

    and a warning about gets, I would recommend changing gets() to a scanf()

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    and a warning about gets, I would recommend changing gets() to a scanf()
    If your file name could have embedded spaces I would recommend using fgets() instead, be sure to remove the end of line character, if present. But never use gets() this function is so dangerous it has actually been removed from the current C standard.

    Jim

  5. #5
    Registered User
    Join Date
    Feb 2015
    Posts
    83
    how did you enter your file, like what was the name of your file

    mine was bacon.txt

  6. #6
    Registered User
    Join Date
    Feb 2015
    Posts
    83
    heres what im getting

    Enter the name of file you wish to see
    bacon.txt
    Error while opening the file.
    The contents of bacon.txt file are :
    Segmentation fault

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ashley1nonly
    I am used note pad and i saved my file as bacon.txt
    Just a wild stab in the dark, but do you have file extensions visible to you when viewing the folders? Sometimes, newbies make the mistake of naming their files "bacon.txt.txt" by providing "bacon.txt" as the filename, upon which certain editors may (un)helpfully append a ".txt" extension to arrive at "bacon.txt.txt" instead of "bacon.txt".
    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. #8
    Registered User
    Join Date
    Feb 2015
    Posts
    83

    t

    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;
    }
    
    
    
    
    
    
    
                                                                                     [ Read 29 lines ]
      g++ read2.c
      ./a.out
    Enter the name of file you wish to see
    read.txt
    Error while opening the file.
    The contents of read.txt file are :
    Segmentation fault

  9. #9
    Registered User
    Join Date
    Feb 2015
    Posts
    83
    It still didnt work, I even made another note pad document with different numbers and name it read. I then typed in when prompted read.txt

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    and a warning about gets, I would recommend changing gets() to a scanf()
    Terrible advice. Without adequate additions for protection, the second could be as bad as the first.

    "fgets()" is usually best for getting a string input (see this FAQ).

    For why "gets()" is bad, see here: FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com

    In your latest example, you're not exiting if the file can't be read. You continue to try processing it, even if it failed to open.

    And what compiler (and if applicable, IDE) are you using?
    And where are you storing your text file?

    It might simply not be in the right directory.

  11. #11
    Registered User
    Join Date
    Feb 2015
    Posts
    83
    i am using ssh secure and I am storing it in my notepad my library documents saved as read

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by ashley1nonly View Post
    i have windwos 8
    i checked my pc
    checked my local c
    checked everything in my apps
    checked everything in other shared computers and still didnt find it
    Quote Originally Posted by ashley1nonly View Post
    i am using ssh secure and I am storing it in my notepad my library documents saved as read
    Wait...are you SSH'ing to another machine and compiling/running your code on the other machine? If so, the bacon.txt will be on that machine, not on your PC. You will have to edit the contents on that machine.

  13. #13
    Registered User
    Join Date
    Feb 2015
    Posts
    83
    ok i went onto my transfer folder on ssh secure and i found the bacon.txt file and it did say hello world )))))))))))))))

    when i am adding in my functions to find slope, distance, and midpoint where would i add them in my function

  14. #14
    Registered User
    Join Date
    Feb 2015
    Posts
    83
    replaced the hello world with numbers and the numbers showed up.

  15. #15
    Registered User
    Join Date
    Feb 2015
    Posts
    83
    how do i make the program read the values for x1 y1 x2 and y2. or will it automatically do that

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