How to read a text based data file into an array in C? - c

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:
Ask user to input file name (I understand this is just a simple printf job)
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.
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.

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.

Related

Read own source file and text file line number?

We have a assignment and the teacher doesn't go into depth with explaining things so I'm a bit confused since I haven't really done much programming before. We have to write a program that when it's done being executed it's able to read its source file and can make another text file which is the same as its source file but the text file has a line number. My problem is I don't understand how to begin it. Could someone give me an example how to get started and what steps to take? I'm not asking for someone to do the programming for me just give an example. Thanks in advance.
Roughly the steps you'll want to take are:
Read each line of the input text file
Prepend the line number to the beginning of each line.
Write your modified lines into a new text file.
There's a lot of good information on how to read/write to files here, and string concatenation (for how to prepend the line number) here. You may also want to look into for loops so that you can hit every line in the input file.
There are really two parts to your question: "Who am I?" (what file are you) and "Write a copy of myself with line numbers"
The part that you describe above is the first -- "Who am I?" and for that, something external to your source code has to provide the info because the language itself can reside in any file.
Often, there is information available about what's being compiled made available by the preprocessor (just like it sounds, it's something that is run before compiling your source code). In this case, "preprocessor macros" commonly give you this sort of environmental data.
Take a look at this link for GNU C: https://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html to start researching what is available under what conditions. Your compiler, if not gcc, should have similar docs.

Reading text from a file in c

So I am trying to write a program that reads random lines of text from an input file. I can open the file but I dont know how to read characters yet, let alone character strings (only numbers so far). I am trying to make it so it can read in random lines of text and then I can manipulate them (i.e. print them in any order)
And is it possible for the program to recognize spaces (or even better periods) in between words in the input file? For example could I make it stop reading after the end of a sentence?
I am not so much looking for someone to write the code for me or anything, I am using this project as kind of a learning exercise so if anyone could tell me what topics in c to study to make this possible that would be great!
Thanks!
Reading your file with fscanf() is a good start. Have a look at the man page which will tell you how to read this either one variable at a time or many variables at a time. If your file is more free form than that, you may want to read the whole thing into memory and then process into tokens (perhaps using strtok or strtok_r), or use a combination of fscanf then process strings you have read with strtok or strtok_r. That should give you a start.

Basic C operations

I have this task that I'm stumbling upon. I'll first go ahead with the description and requirements for it and then point out what I'm having trouble coping with.
Build a block scheme and a program that reads another C-based program and finds:
Number of lines in the program
Number of conditional operators if and if/else
The program needs to start with a menu disposing with the following options:
Reading the program from a file and storing the result in a separate file (the user has to input the names of both files whilst the program
file has to end in “.C”)
Reading the program from a file and outputting the result on the screen (the user selected file has to end in “.C”).
Reading of the program from the keyboard and inserting a file selected by the user;
Reading of the program from the keyboard and outputting on the screen; The program has to be written in different functions for each
operation.
Reading of the file has to be done line by line.
First question is what is a block scheme and what 'reading from a keyboard' refers to?
Thanks in advance
A block scheme is another, albeit less used, term for a block diagram, or a flow chart.
A block diagram is:
a diagram of a system in which the principal parts or functions are represented by blocks connected by lines that show the relationships of the blocks.
Or in other words, it's a way of using pen and paper to construct what will be the flow of your program prior to writing any code. Typically they used a set of shapes to mean certian things, circles/ovals can show states/starts/ends, a rectangle may repersent a function, a diamond could be used for a decision point, etc.
As you'er asking about a specific homework problem it is best to clairify what your instructor expects, but a quick example of a block scheme for a progrm could be something like:
This maybe too algorithimic for what your instcutor wants, they may just want to see blocks only stating "get input from user", "open a file" showing the flow at a moduler level and ignorning the decision details as in checking error conditions.
Now as far as "•Reading of the program from the keyboard", as I didn't write the assignment, it's again speculation, but I'm pretty sure your instructor is simply saying get input via stdin. The mechanism would be dependent on what you're learning in class, but in C, just something like scanf() or fgets() or whatever you know can get the input from the keyboard.

How to take numbers and words out of a txt file and assign them to int and char*

I am making a text based game and want the user to be able to save. When they save all the variables will be saved to a text file.
I can't figure out how to take them out of the file and assigning them to specific variables and pointers.
The file will look somewhat like this:
jesse
hello
yes
rifle
0
1
3
20
Is there anyway I can specify what line I want to take out with fscanf? Or do I have to take a different approach?
There is no way to specify what line to read from because the concept of a file stream in C does not explicitly distinguish new lines. They are simply treated as a character. To read from a specific line, you would have to loop forward with fseek and fgetc until you find '\n' at which point you can update some variable that holds the current line number the stream points to.
One way around this would be to have information at a fixed offset. For example, say you are storing player information then if you make player information a fixed size X and have the constituent data at fixed indexes into each structure, you can just fseek to the right location straight away.
However, if you have structured data, it may be more suitable to use a format which is able to represent these structures inherently such as XML or JSON.
Altough I can't exactly tell what you want, I'd do a few suggestions:
Use a SQLite file instead of a text file. This way you can use SQL to get exactly what you want. Shortcut for you: http://www.sqlite.org/
If you still want to use a text file, use it comma-separated instead of spaces-separated. It's more common of a practice.
I have created a simple settings reader for my C program, maybe it might be useful to you to know how to parse test files
https://codereview.stackexchange.com/questions/8620/coding-style-in-c

Simple C question

HI all. I am trying to make little program that reads data from file which has name of user and some data for that user. I am new on C , and how can i calculate this data for its user?line by line reading and adding each char in array? And how can I read line? is there any function?
And how can I use this each line users like object?I will make calculation for specific user.
You can use fgets to read a line at a time from the file.
Then you can parse the fields out and add them to an array or some other data structure. Just keep in mind if you use an array then you need to know ahead of time how many entries the file may contain - for example, no more than 1000. Otherwise you will need to use a data structure that can dynamically allocate memory such as a linked list, vector, etc.
Try this site, i often use it for reference.
http://www.cprogramming.com/tutorial/cfileio.html
Play around with file i/o and get use to the functions and then you will be able to make what you want.
Everything you need is in stdio.h. (Link is to a C++ website but the entire C i/o system is usable in C++; hence the documentation)

Resources