Thread: Read input.txt into a c file

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    23

    Read input.txt into a c file

    I'm new to programming and I have a question about scanning numbers from a .txt file into a .c file. I have an array that has 50 integers and I want to read in 50 numbers from a .txt file. What would I type in on the command line to get the array to read in the .txt numbers?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It depends on the file format. You have say, 50 integers in a text format separated by spaces, or with each integer on its own line, you could use fscanf (or a combination of fgets and sscanf) in a loop to read them into the array.

    As for typing on the command line: perhaps you could make the file name be a command line argument.
    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. How to read this file input
    By plot in forum C Programming
    Replies: 4
    Last Post: 11-12-2013, 10:28 PM
  2. Input CSV File and Read Data
    By Daive in forum C++ Programming
    Replies: 0
    Last Post: 04-15-2012, 01:15 PM
  3. read from input file
    By imjustnew in forum C++ Programming
    Replies: 5
    Last Post: 12-28-2011, 01:32 PM
  4. Beginner Help Read Next Line of Input File
    By SkinnieMinnie in forum C++ Programming
    Replies: 10
    Last Post: 11-25-2007, 01:23 AM

Tags for this Thread