How to read and compare strings from dynamic output console in C Programming?
Consider, you will get one output at a time. You need to consider the output from a Program as an input to other program function. The example output of the Program 1 as follows,
A123#345H5KGH3***STATE***GETRUNOMMUNICATION5619***CONNECTED***HIEDSAKNFH***OK***
I need to get the statement STATE:CONNECTEDOK if this statement present in the output console, then the next program(function) will start.
Note:
I am getting one output at a time
Consider the first output as "A" the next one will be "A1" and the following output as "A12".
I thought to use array to store values, but the output range( unlimited) makes it very lengthy and difficult to configure before hand ,since the length of the array is unknown.
If the specified statement appears in the output console, then it should prompt some message.
No spaces in between the two continues outputs.
I thought to use strings, so my question are as follows,
How to read the string from output console, when the length of the string is unknown?
What function I need to use to perform the comparison ( I already know the statement "STATE:CONNECTEDOK") with unknown length of output?
How to store the output data, when the range or length is unknown?
Thank you.
Assuming you don't mean that you want to identify the data graphically from the actual console window, but instead that you can pipe the data to your program, I think the best bet is to use a state machine, namely an "acceptor".
You simply read a character at a time, and feed it into the state machine.
Related
I'm working on an assignment that is supposed to go over the basics of reading a file and storing the information from that file. I'm personally new to C and struggling with the lack of a "String" variable.
The file that the program is supposed to work with contains temperature values, but we are supposed to account for "corrupted data". The assignment states:
Every input item read from the file should be treated as a stream of characters (string), you can
use the function atof() to convert a string value into a floating point number (invalid data can be
set to a value lower than the lowest minimum to identify it as corrupt)."
The number of elements in the file is undetermined but an example given is:
37.8, 38.a, 139.1, abc.5, 37.9, 38.8, 40.5, 39.0, 36.9, 39.8
After reading the file we're supposed to allow a user to query these individual entries, but as mentioned if the data entry contains a non-numeric value, we are supposed to state that the specific data entry is corrupted.
Overall, I understand how to functionally write a program that can fulfill those requirements. My issue is not knowing what data structure to use and/or how to store the information to be called upon later.
The closest to an actual string datatype which you find in C is a sequence of chars which is terminated by a '\0' value. That is used for most things which you'd expect to do with strings.
Storing them requires just sufficent memory, as offered by a sufficiently large array of char, or as offered by malloc().
I think the requirements of your assignment would be met by making a char array as buffer, then reading in with fgets(), making sure to not read more than fits into your array and making sure that there is a '\0' at the end.
Then you can use atof() on the content of the array and if it fails do the handling of corrupted input. Though I would prefer sscanf() for its better feedback via separate return value.
I am trying to form a pipe, where a program written in C gives its continuous output with write(). This stream of data shall be accepted by python script and processed further.
By using input() (python 3.x) I was able to catch the data when C-written source program was giving its data out with printf(), anyway to speed up I changed to write() to console.
And this is where the problem start: From now on I am unable to fetch the data with python script, because input() doesn't want to accept non-ascii values. I know, write() gives a binary data instead of strings.
Is there any other way for python to accept the data from outside, preferably from console, where data consists non-ascii values (preferably binary input)?
I have tried to modify the C program to form a ascii string. The array of chars plus terminating zero (also: zero-terminated string, with CR and LF+CR), anyway python seems to read further behind terminating zero and then giving UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 130: invalid continuation byte error, where sent string was 4 characters long (C - array: [99, 0, 10, 13] which is equivalent to 'c' zero-terminated and LF and CR).
So - trying to implement other solution - question 2. Is there a possibility to limit the length of input() before python grabs unknown portion of data from console and throws errors?
For reading binary, you should probably use the .read() method on a file object. And the object you want to use is probably sys.stdin.buffer.
Check the docs for the io module, and for sys.stdin.
For your first question, the documentation for sys.stdin would be helpful here. In particular there is a very helpful note contained in the documentation:
Note: To write or read binary data from/to the standard streams, use the underlying binary buffer object. For example, to write bytes to stdout, use sys.stdout.buffer.write(b'abc').
For you second question, the default input function reads in lines at a time.
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.
I am trying to 'prettify' some code that makes a log file and in the log file I want to print the name of the input files and what settings were used in a nicely formatted #-box. I need to be able to get the length of an entered argument which has to be stored by the GNU getopt somewhere for it's internal malloc, at least I assume that's how they did it..
Does anyone know how to get that value? I could then use some simple calculus to see howmuch whitespace that I need to add after the last letter of text in my comment box ;)
Where your code stores the value, it is available in the
extern const char *optarg;
variable, and you can simply use strlen() to compute the length of the string.
I have taken up a project and I would like some help. Basically it is a program to check whether some pins are connected or not on a board.
(Well, that's the simplified version. The whole thing is a circuit with a microcontroller.)
The problem is that, when a pin is connected I get a numeric value, and when it's not connected, I get no value, as in it's a blank in my table.
How can I accept these values?
I need to accept even the blank, to know that its not connected,
plus the table contains some other non-numeric values as well.
I tried reading the file using the fscanf() function but it didn't quite work. I'm aware of only fscanf(), fread(), fgets() and fgetc() functions to read from different kinds of files.
Also, is it possible to read data from an Excel file using C?
An example of the table is:
FROM TO
1 39
2
Over here, the numbers 1 and 2 are under the column FROM and it tells which pin the first end of the connector is connected to. The numbers under TO tell us which pin the other end of the connector is connected to, and when the column is blank, it's not connected at one end.
Now what I'm trying to do is create a program to create an assembly language program for the micro controller, so I need to be able to read whether the connector is connected, and if it is then to which pin? And accordingly, I need to perform some operations. (Which I can manage by myself).
The difficulty I'm facing is reading from a specific line and reading the blank.
Read the lines using fgets() or a relative. Then use sscanf() on the line, checking to see whether there were one or two successful conversions (the return value). If there's one conversion, the second value was empty or missing; if two, then you have both numbers safely.
Note that fscanf() and relatives will read past newlines unless you're careful, so they do not provide the information you need.
so your file is more like this
Col1 col2 \n
r1val1 r1val2\n
.
.
and so on,if this is the case then use fscanf() to read the string (until \n)from the file.Then use strtok() function to break the string into tokens ,here is the tutorial of the same
http://www.gnu.org/s/hello/manual/libc/Finding-Tokens-in-a-String.html
hope this helps...
one more humble suggestion..just work on c programming first if you are a newbie,don't directly go for microcontrollers,as there are lots of things that you might understand in a wrong way if you dont know some of the basic concepts...
This is a common problem in C. When line boundaries carry meaning in the grammar, it's difficult to directly read the file using only the scanf()-family functions.
Just read each line with fgets(3) and then run sscanf() on one line at a time. By doing this you won't incorrectly jump ahead to read the next line's first column.
Since there are two values on a line you can parse the first, find the next whitespace, then parse the next looking for it's absence as well. I say parse rather than scanf() as when I really want control, or have a huge volume of numbers to scan, I use calls in the strtol() family.