-2

I have to read a text based data file, with an unknown number of data points, into an array in C, but I can't work out how to do this. I can't even manage to get my program to successfully open the text file, let alone put it into an array etc

The file contains numerical values, so it is not a string it needs to be read into. Ideally this should be done by the user inputting the file name.

I basically need the program to:

  1. Ask user to input file name (I understand this is just a simple printf job)
  2. When the user inputs the file name, the program opens the text file, stores the data from it into an array of an appropriate size.
  3. Print entire array to show that this has been done.

If anyone could give a step to step explanation of how this can be done I would really appreciate it.

7
  • Does each row have an unknown number of numbers, or is that number fixed? And if you haven't worked with pointers before, now is the time to start. Commented Sep 7, 2015 at 11:11
  • 6
    Show the code you've written so far. Commented Sep 7, 2015 at 11:11
  • A simple approach is to read the file twice. 1st time just to get the data count. Either way, suggest posting what you have tried. Commented Sep 7, 2015 at 11:15
  • Use the fscanf function to read numbers, and the realloc function to reallocate array in the RAM. Commented Sep 7, 2015 at 11:18
  • Possible duplicate of stackoverflow.com/questions/27856886/… Commented Sep 7, 2015 at 11:20

1 Answer 1

0

Anything asked to be described step by step without asking your input would be copy of others work. Best advice is to learn things step by step on your own.

File I/O in C: http://www.tutorialspoint.com/cprogramming/c_file_io.htm

If you want to add additional features like user input:How to read a string from user input in C, put in array and print

Do some research on file content and how it's being handled from program. (Seems that you are referring to ASCII format file).

You should have done some searching before asking this complexity level questions. If you want same advice in future for this task, I suggest to add code here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.