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

  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,465
    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
    27,390
    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
    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

  9. #9
    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

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    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
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,390
    I suggest that you try:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
        /* Write "hello world!" to "bacon.txt" */
        char text[100];
        char file_name[100] = "bacon.txt";
        FILE *fp = fopen(file_name, "w");
        if (!fp)
        {
            fprintf(stderr, "Could not open file for writing.");
            return 0;
        }
        fprintf(fp, "%s\n", "hello world!");
        fclose(fp);
    
        /* Read from "bacon.txt" */
        fp = fopen(file_name, "r");
        if (!fp)
        {
            fprintf(stderr, "Could not open file for reading.");
            return 0;
        }
        
        if (fgets(text, sizeof(text), fp))
        {
            text[strcspn(text, "\r\n")] = '\0'; /* remove newline if it exists */
            printf("%s content: %s\n", file_name, text);
        }
        else
        {
            fprintf(stderr, "Read error.");
        }
        fclose(fp);
    
        return 0;
    }
    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

  13. #13
    Registered User
    Join Date
    Feb 2015
    Posts
    83
    but how would that help me open my files with numbers. I'm supposed to write a program that accepts two ordered pairs (x2,y2) and (x1,y1) read from a file. The program should compute the slop, distance between two point and the midpoint. Accepting a max of 100 numbers

    Do you know whats wrong with min

  14. #14
    Registered User
    Join Date
    Feb 2015
    Posts
    83
    i tried fgets and it messed up my program

  15. #15
    Registered User
    Join Date
    Feb 2015
    Posts
    83
    your program did work when i ran 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