Basic C operations - c

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.

Related

How can I reuse a text file in a C program without having a file pointer?

My program has uses a text file to input data, but can't use a file pointer in the program itself. I'm supposed to use the < file.txt in the Linux terminal. I can't do it any other way because it's a college assignment, so please don't waste my time with rewind or other functions that require a pointer. I just need to be able to basically restart the text file that I already have open.
The C library provides a FILE pointer for standard input, stdin from <stdio.h>. However, it might not support all the functions you want, since it can be connected to another command (if you pipe your input from somewhere else) or the terminal (if you don't use input redirection). If you need to be able to support these, which you probably do, you won't be able to successfully call fseek or any of the related functions.
If that's the case, then this is fundamentally impossible. The computer doesn't store all the data which was sent to your program, so there is no way to go back and get it because there's nowhere to get it from. Instead, you either need to store the input yourself, or rework your algorithm to only need a single pass over the input data.

Should I keep source files in memory while parsing?

I'm writing the front-end part of an interpreter and I initially disliked the idea of just dumping all the source files into memory and then referencing that text directly. So the tokenizer reads from a char buffers and builds the token stream.
However, I have reached the parsing side of things and it hit me that because I would want to output nice errors and warnings that show the malformed line of source code. I guess I could put column numbers in the tokens, but then by error messages would be like getting directions by telephone: "It's in file X, on line Y, column Z, right next to the curly brace, you know the one. If you hit the semicolon, you've gone to far."
I seem to have put myself into a situation where I want to have my cake and eat it too. I want nice messages, but I don't want to hog memory.
It there something I'm missing? Or is loading the source in memory the way to go?
When there's an error to report to the user, it hardly matters how long in milliseconds it takes to report it.
I'd keep your tokenized stream in memory to keep your interpreter fast. (Actually, you should switch to a threaded interpreter or even a bad one pass compiler to enhance the execution rate).
When you encounter an error, go to the disk, fetch the line(s) of interest, and show them to the user. If he doesn't make any errors, this costs you zero. If he makes a small number of errors, that may be tiny bit inefficient but the user won't know. If he makes large number of errors, the file content of the files containing errors are going to read by the OS into its local cache, which is likely bigger than your programs anyway, and so access will be more efficient than if you kept the source entirely on the disk.
Better idea: mmap your sources in the first place, if you can. Fall back to slurping the whole file if you're reading from a pipe or something.
After parsing, you may want to call madvise(MADV_DONTNEED) (but only if it was originally mmaped) to advise the kernel to drop it from the cache (but still keep it available for errors) ... but this is probably not necessary, and may even not be a good idea, depending on your compiler design (e.g. are identifiers still pointing, or are they interned to a single, separate, allocation).

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.

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.

File handling and Graph ADT in C

I created a Thesaurus program in C.
In my program user can insert a word and the synonym for that.
Another function is searching for a word and then displays the synonyms for that word.
My question is how can I keep the words I have inserted and still retrieve them when I run the program again?
Is file handling a solution?
How will I do it?
You need to design a simple file format which could describe your data, then write code to write to that format, and code to read from that format and handle errors properly.
As a simple example, you could have a file which stored lines like:
happy:joyful
happy:exuberant
In this case you would also need to make sure that users can't enter blank lines or colons as word input, so that the syntax is unambiguous.
A program cannot reliably keep information in memory between runs*, so it has to store such information in a file. Files are designed to store information between runs of a program.
As to how you'll do it, that's your decision. Most likely, you'll choose a simple and readable format with, for example, the head word at the start of a line, followed by a colon, then a list of semi-colon separated synonyms:
head: skull; cranium; noggin; noodle
head: aptitude; faculty; talent; gift; capacity; ability; mind; brain
This is flexible and allows you to use phrases (even phrases containing commas) in the synonym lists. You can sort the data before you write it out for convenience when reading in, but it is generally best to validate that the data is still sorted when you read it back in (at the start of the next run) because someone may have hand-edited the file and not preserved sorted order.
* If the process uses System V shared memory IPC, then you could store the data in a shared memory segment that would exist between runs of the program. However, it is not a particularly sensible idea to try doing that. A file has better durability; it will (usually) survive reboots, and could be placed on a distributed file system whereas shared memory is restricted to a single machine.

Resources