How to change a specific string in a character Array? - c

This is my very first question so it might look a bit ugly.
Problem: I have to be able to read and replace names.
My solution:
*char NamesArray[] {"name1", "name2"};
so I could just enter in names and read them through pointers.
Although reading full names requires me to make a loop now.
This does bring another problem and that is, not being able to change names when program is running.
How do I change names and how do I print the new list of names after one or more names changed?
Its not a must to have names already in the array.
If its possible with a different array or/and library, that's fine.
Name inputs are given through terminal but that's probably not a surprise.
Thank you in advance!

Related

How to read in variable names and their corresponding values in C

I've been searching all over and cannot figure out how to do this.
I am trying to read in a text file with contents like
x 4
y 6
z 9
and set the int values to the corresponding variable name before it;
is there anyway to do this or do I have to assign the names in the program after reading in the values.
There are a couple of methods you could use to get the data into the program associated with a name, depending on how your program is structured.
If your program doesn't contain variables with those names already, then there's not a way to generate variables from a file. You could create a dictionary mapping the names to the values and use that as your "variable".
I suppose you could also have a dictionary mapping the names to the addresses of existing variables... so that you look up the name and then deref the variable to set if its in the dictionary.
As a really far-out solution... if you can recompile each time there is a new file (ie. the file is really compile-time input, not run-time input) then you might be able to use some kind of preprocessing to #include the (possibly itself preprocessed into c-code) file of data.
I think you are asking for dynamic code generator those I used for different purpose back then (generating long byte arrays with certain values). The generator will generate a .c file with variable names read from another file. This problem is, it's hard to do this for runtime, if you asking for runtime solution, other dudes are already given the answer.

How to read a text based data file into an array in 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.

quiz questions from txt file into array in c

I am trying to get the line below into an array in c. this is for a quiz program that asks 10 questions (out of possible 15) in a random order and tracks the score and tells the user at the end how they did.
here is an example of the format for one line in my text file:
what is not an official language in Canada;English;French;Spanish;3
in other words format is string;string;string;string;int
or
question;choice-a;choice-b;choice-c;correct answer
I know how to read a file line by line in C but I do not know how to utilize these ; to divide up one line into multiple lines. Also, not sure how to keep score for this quiz after user selects their choice as an int and presses enter.
Apparently, I am supposed to define a struct to present these questions...
Any help would be greatly appreciated. Yes, this is for a final project but lectures now are over and our teacher will not provide anymore help to students outside of class hours.
Here is what I have done so far:
http://i.stack.imgur.com/E8LtH.png
Since this is for a class, I don't know if your instructor wants you to use low-tech methods or not, but I would use something like sscanf for a problem like this. You can do something like this:
int number;
char fruit[16];
char line[]="I ate 3 bananas.";
sscanf(line, "I ate %d %s.", &number, fruit);
And it updates the variables that you point to within the sscanf function accordingly.
As this is for a class project, I will provide only general guidance. I would suggest you review the strtok function, using the line from your text file as the string and a ";" as the token. Examples and documentation are available here. Repeated calls to this function will allow you to divide your line up into segments.
As for the struct requirement, a sensible way would be to create a struct with these members:
A string (char*) containing the question.
An array of strings containing the choices.
An int defining the number of choices (i.e. the length of the array in 2). You might be able to hard-code this to 3 in your example.
An int defining the correct answer.
You would create an instance of the struct for each line in your text file.
What you need is probably a tokenizer.
You may want to have a look at the discussion here Using strtok in c. A few snippet of code are listed which in my opinion, is very helpful in your case.

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