Thread: Read file data and store to an array

  1. #16
    Registered User
    Join Date
    Mar 2021
    Posts
    9
    Thank you. Something I still can't understand and if you clear it for me I'd appreciate. Why is fgets returning -1?(even after deleting the code you mentioned). I understand that fgets would be NULL if the file is empty or EOF
    Last edited by aidan4; 03-06-2021 at 07:23 PM.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by aidan4
    Thank you. Something I still can't understand and if you clear it for me I'd appreciate. Why is fgets returning -1?(even after deleting the code you mentioned). I understand that fgets would be NULL if the file is empty or EOF
    Oh, but now that's something else. It is not fgets itself that returns -1: it is the code in the body of the read_coords (or read_file) function that returns -1, if fgets returns a null pointer.

    This should not be happening since the file should have the content that you described. Since it is happening yet you can open the file for reading, it implies that the file is actually empty. If you really do believe that the file contains something, then what I suggest is that you post the program that you're testing with (it should be a small and contained example like my program from post #8) as well as the entire test input file (post it in code tags).
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read data from file and store in array
    By meagangramlin in forum C++ Programming
    Replies: 1
    Last Post: 11-25-2020, 06:00 PM
  2. How to read file and store into an array
    By ec661 in forum C Programming
    Replies: 5
    Last Post: 05-06-2019, 02:47 AM
  3. Read text file, scan and store data into variables?
    By wisdom30 in forum C Programming
    Replies: 8
    Last Post: 04-18-2011, 11:23 PM
  4. Read text file and store it in an array
    By look2hook in forum C Programming
    Replies: 2
    Last Post: 12-03-2010, 11:47 PM
  5. read and store text file as an array
    By abotaha in forum C++ Programming
    Replies: 1
    Last Post: 08-02-2010, 08:57 PM

Tags for this Thread