I am a newbie to programming in postscript directly.
I am reading from a file and I am using the read command to parse symbols.
Most of the symbols I am checking for are 2 characters in length and one is 3 characters in length. I would change the one that is 3 characters in length into two characters, but that won't help for the following reasons:
The symbols are a standard so I can't say.. well, thanks for making
just one of them 3 characters unlike all the rest!
The symbol that has 3 characters has the first 2 characters the same as another symbol.
I need to be able to "peek" at the next character in the file if the first two symbols match and not modify the the read order if the character peeked at doesn't match what I want to make the 3 character symbol.
Example:
File text as a string: "AB3;ABB4;"
In this case I will read A, then B and because the two characters together are "AB", I need to see if reading the next character produces a B.. if not, I don't want to have modified my reading order so I can proceed in the code normally to extracting the value. If it does, I now have my 3 character symbol and can proceed to extract the value normally.
Thank you.
Postscript doesn't have an unget function, so you can't push a byte back onto the stream to read it again later. But, depending upon OS-support, Level 2 Postscript lets you reposition a file. So you could implement your peek function like this:
% file peek int true
% false
/peek {
dup fileposition exch dup % pos file file
read { %true: pos file int
3 1 roll exch setfileposition true
}{ %false: pos file
pop pop false
} ifelse
} def
Related
I came across the following question:
If a file contains the line "I am a boy\r\n" then on reading this line into the array str using fgets(). What will str contain?
[A]. "I am a boy\r\n\0"
[B]. "I am a boy\r\0"
[C]. "I am a boy\n\0"
[D]. "I am a boy"
The answer has been given as option c with the explanation
Declaration: char *fgets(char *s, int n, FILE *stream);
fgets reads characters from stream into the string s. It stops when it reads either n - 1 characters or a newline character, whichever comes first.
However, I couldn't understand how will \r (carriage return) influence fgets. I mean, shouldn't it be that first "I am a boy" is read, then on encountering \r cursor is set at the initial position and "I" from "I am a body" is overwritten by \n and space following "I" is overwritten by \0.
Any help is deeply appreciated.
P.s: My claim is based on the explanation given on this link: https://www.quora.com/What-exactly-is-r-in-the-C-language
First, every time you see a multiple choice quiz on some programming website, I recommend you close the tab and do something productive instead such as watching videos of kittens. Because the questions seem to be just some variants of
Which of these is the first letter of the alphabet (only one is right)
A
a
6
a
the letter a
all of the above.
Carriage returns and line feeds do not affect the input read by a C program in that way. Each additional byte is just on top of the other bytes. Otherwise, this is very badly phrased question, as the answer be any of A, B, C or D, or maybe none of them. Saying that C is the only one that is right is wrong.
First question is what it means if "the file contains \r"? Here I assume that the author meant that the file contains the 10 characters I am a boy followed by ASCII 13 and ASCII 10 (carriage return and line feed).
In C there are two translation modes for reading files, text mode and binary mode. On POSIX systems (all those operating systems with X in their name, except for Windows eXcePtion) these are equal - the text mode is ignored. So when you read the line into a buffer with fgets on POSIX, it will look for that line feed and store all letters as is including the , so the buffer will have the following sequence of bytes I am a boy\r\n\0. Therefore A could be true.
But on Windows, the text mode translates the carriage return and the linefeed to one newline character with ASCII value 10 in memory, so what you will have is I am a boy\n\0. Therefore C could be true. If your file was opened in binary mode, you'll still have I am a boy\r\n\0 - so how'd you claim that C is the only one that can be true?
If the string that you'd read with fgets would be I am a boy\r\n (POSIX or binary mode) but you told fgets your buffer has space for only 12 characters, then you'd get 11 characters of the input and terminating \0, and therefore you'd have I am a boy\r\0. The carriage return character would remain in the stream. Therefore B could be true. B cannot be true if you indicated that the buffer will have more space.
Finally any of these array contents does contain the string I am a boy, therefore D would be true in all of the cases above.
And if your buffer didn't have enough space for 10 characters and the terminator then you'd have some prefix of the contents, such as I am a bo followed by \0 which means that none of these was true.
Say I'm calling a program:
$ ./dataset < filename
where filename is any file with x amount of line pairs where the first line contains a string and second line contains 10 numbers separated by spaces. The last line ends with "END"
How can I then start putting the first lines of pairs (string) into:
char *experiments[20] // max of 20 pairs
and the second lines of the pairs (numbers) into:
int data[10][20] // max of 20, 10 integers each
Any guidance? I don't even understand how I'm supposed to scan the file into my arrays.
Update:
So say this is my file:
Test One
0 1 2 3 4 5 6 7 8 9
END
Then redirecting this file would mean if I want to put the first line into my *experiments, that I would need to scan it as such?
scanf("%s", *experiments[0]);
Doing so gives me an error: Segmentation fault (core dumped)
What is incorrect about this?
Say my file is simply numbers, for ex:
0 1 2 3 4 5 6 7 8 9
Then,
scanf("%d", data[0][0]); works, and will hold value of '1'. Is there an easier way to do this for the whole line of data? i.e. data[0-9][0].
find the pseudo-code, code explains how to read the input
int main()
{
char str[100]; // make sure that this size is enough to hold the single line
int no_line=1;
while(gets(str) != NULL && strcmp(str,"END"))
{
if(no_line % 2 == 0)
{
/*read integer values from the string "str" using sscanf, sscanf can be called in a loop with %d untill it fails */
}
else
{
/*strore string in your variable "experiments" , before copying allocate a memory for the each entry */
}
no_line++;
}
}
The redirected file is associated with the FILE * stdin. It's already opened for you...
otherwise, you can treat it the same as any other text file, and/or use the functions that are dedicated to standard input - with the only exception that you cannot seek in the file and not retrieve the size of the input.
For the data sizes you're talking about, by far the easiest thing to do is just slurp all of the content into a buffer and work on that: you don't have to be super-stingy, just make sure that you don't overrun.
If you want to be super-stingy with memory, preallocate a 4kB buffer with malloc(), progressively read() into it from stdin, and realloc() another 4kB every time the input exceeds what you've already read. If you don't care so much about being stingy with memory (e.g. on a modern machine with gigabytes of memory), just malloc() something much bigger than the expected input (e.g. a megabyte) and bug out if the input is more than that: this is far simpler to implement but less general/elegant.
You then have all of the input in a buffer and you can do what you like with it, which depends too strongly on the format of the input for me to say how you should approach that part.
I am looking for logic and code to read file and print the lines between two pattern matched. Both pattern could be in one line or may be in next consecutive line or 1st pattern appear at line number "n" and 2nd pattern appeared in line number "m" or 2nd pattern appear in line number "n" and 1st pattern appear in line number "m".
One way to achieve is to read file and track two int variable which will note the line number where first pattern match and other pattern match. For example variable x, y. x will record line# of first patter and y will record line# of second pattern, close the file. then check condition of x=y or x> y or x < y and based on that reread file and print lines between x and y or y and x. Not sure if I can reread the file before closing it or not?
Other way is to write if condition with (string compare(pattern1 with current line) || string compare (patter2 with current line) && (string compare(pattern1 with current line) || string compare (patter2 with current line)... not sure if this will work or not so need some feedback or pointers on how to achieve it in C programming.
You can use your first approach with fpos_t variables to track the positions in the file at which each of the patterns match, obtaining the values of those variables with fgetpos() when you've found a match. You can then use fsetpos() to set the position in the file back to the point that you recorded from the first match to re-read those lines and print out the ones you want. There is no need to close and re-open the file.
You can find information on these functions in fgetpos/fsetpos and ftell/fseek and an example of using fgetpos() and fsetpos() here.
I am new to C programming, so I am having difficulties with the problem below.
I have a text file inp.txt which contains information like the following:
400;499;FIRST;
500;599;SECOND;
670;679;THIRD;
I need to type a number and my program needs to compare it with numbers from the inp.txt file.
For example, if I type 450, it's between 400 and 499, so I need write to the word FIRST to the file out.txt
I have no idea how to convert a character array to an int.
I think you'll want these general steps in your program (but I'll leave it to you to figure out how you want to do it exactly)
Load each of the ranges and the text "FIRST", "SECOND", etc. from the file inp.txt, into an array, or several arrays, or similar. As I said in the comment above, fscanf might be handy. This page describes how to use it - the page is about C++, but using it in C should be the same http://www.cplusplus.com/reference/clibrary/cstdio/fscanf/. Roughly speaking, the idea is that you give fscanf a format specifier for what you want to extract from a line in a file, and it puts the bits it finds into the variables you specify)
Prompt the user to enter a number.
Look through the array(s) to work out which range the number fits into, and therefore which text to output
Edit: I'll put some more detail in, as asker requested. This is still a kind of skeleton to give you some ideas.
Use the fopen function, something like this (declare a pointer FILE* input_file):
input_file = fopen("c:\\test\\inp.txt", "r") /* "r" opens inp.txt for reading */
Then, it's good to check that the file was successfully opened, by checking if input_file == NULL.
Then use fscanf to read details from one line of the file. Loop through the lines of the file until you've read the whole thing. You give fscanf pointers to the variables you want it to put the information from each line of the file into. (It's a bit like a printf formatting specifier in reverse).
So, you could declare int range_start, range_end, and char range_name[20]. (To make things simple, let's assume that all the words are at most 20 characters long. This might not be a good plan in the long-run though).
while (!feof(input_file)) { /* check for end-of-file */
if(fscanf(input_file, "%d;%d;%s", &range_start, &range_end, range_name) != 3) {
break; /* Something weird happened on this line, so let's give up */
else {
printf("I got the following numbers: %d, %d, %s\n", range_start, range_end, range_name);
}
}
Hopefully that gives you a few ideas. I've tried running this code and it did seem to work. However, worth saying that fscanf has some drawbacks (see e.g. http://mrx.net/c/readfunctions.html), so another approach is to use fgets to get each line (the advantage of fgets is that you get to specify a maximum number of characters to read, so there's no danger of overrunning a string buffer length) and then sscanf to read from the string into your integer variables. I haven't tried this way though.
I have a file a.txt:
03,17.406199
05,14.580129
07,13.904058
11,14.685388
15,14.062603
20,14.364573
25,18.035175
30,21.681789
50,22.662820
The number of rows in the file are not known. I want to read the file and store
3
5
7
11
15
20
30
50
in one array and the float values in another.
How do I read in a file when the length of data is not known?
If the number of entries is the same in every row, and if all the entries are numeric, then
you can simply do
a = load('a.txt');
a will be a matrix with two columns.
Read line-by-line until you hit the EOF marker.
Certain functions (like TEXTSCAN) will continue recycling the format string until the end of the file is reached. Other functions (like FSCANF) can take Inf as a size option, indicating that it should continue reading until the end of the file. If you are reading data line-by-line in a loop, you can use the FEOF function to test if the end of the file has been reached.
Since your elements are separated by commas, take a look at csvread. This should read the entire file into a single matrix, which you can then split into the two vectors you want.
Disclaimer: not tested!
fileContents = csvread('a.txt');
integerColumn = fileContents(:, 1);
doubleColumn = fileContents(:, 2);