LUA: Need detect number of current line in file - file

I'm reading the file from input by characters and when I read an invalid character, I need to know what line it was, eventually what was the number of the characater on current line. In Lua, is there any function or way to detect this? I haven't got a clue. Thanks!
Or is it possible to count \n or something like that?

Related

How can I search for a specific string in a csv file, and to know in which line that string was found?

I started to learn C and I am writing a code where the user needs to type a string he wants to search in the csv file , and if the string was found , it will print the number of line the string was found at .
and I am not exactly sure what is the best way to do that
You may read line by line using getline() until it returns -1 (EOF), keeping a counter on how many iterations you have done.
For each line you read, you can use strstr() to check if the string is present. If yes, just print the counter (that will correspond to the line).

How Can I Read Specific Parts of a File in C?

I wish to read and write to a .csv file using this format: ~83474\t>wed 19 march 2014\n
When reading, I need to ignore the ~, the tab and the >. They are just there to remind my program of what the values that follow are used for. So far I figured out how to write to file using that format, however, I do not know how to read from the file either. I wish to store the numbers after the ~ as an integer value and the characters after the > as a string. How can I read those two values from every line in the file if each line has the format stated above?
Read the whole line as a string using fgets and process it.

How to read from a specific line from a text file in VHDL

I am doing a program in VHDL to read and write data. My program has to read data from a line, process it, and then save the new value in the old position. My code is somewhat like:
WRITE_FILE: process (CLK)
variable VEC_LINE : line;
file VEC_FILE : text is out "results";
begin
if CLK='0' then
write (VEC_LINE, OUT_DATA);
writeline (VEC_FILE, VEC_LINE);
end if;
end process WRITE_FILE;
If I want to read line 15, how can I specify that? Then I want to clear line 15 and have to write a new data there. The LINE is of access type, will it accept integer values?
Russell's answer - using two files - is the answer.
There isn't a good way to find the 15th line (seek) but for VHDL's purpose, reading and discarding the first 14 lines is perfectly adequate. Just wrap it in a procedure named "seek" and carry on!
If you're on the 17th line already, you can't seek backwards, or rewind to the beginning. What you can do is flush the output file (save the open line, copy the rest of the input file to it, close both files and reopen them. Naturally, this requires VHDL-93 not VHDL-87 syntax for file operations). Just wrap that in a procedure called "rewind", and carry on!
Keep track of the current line number, and now you can seek to line 15, wherever you are.
It's not pretty and it's not fast, but it'll work just fine. And that's good enough for VHDL's purposes.
In other words you can write a text editor in VHDL if you must, (ignoring the problem of interactive input, though reading stdin should work) but there are much better languages for the job. One of them even looks a lot like an object-oriented VHDL...
Use 2 files, an input file and an output file.
file_open(vectors, "stimulus/input_vectors.txt", read_mode);
file_open(results, "stimulus/output_results.txt", write_mode);
while not endfile(vectors) loop
readline(vectors, iline);
read(iline, a_in);
etc for all your input data...
write(oline, <output data>
end loop;
file_close(vectors);
file_close(results);

\n characters before EOF in file causing problems

I have written some data to a file manually i.e. not by my application.
My code is reading the data char by char and storing them in different arrays but my program gets stuck when I insert the condition EOF.
After some investigation I found out that in my file before EOF there are three to four \n characters. I have not inserted them. I don't understand why they are in my file.
Want to remove those pesky extra characters? First, see how many of them there are at the end of your file:
od -c <filename> | tail
Then, remove however many characters you don't like. If it's 3:
truncate -s -3 <filename>
But overall, if it were me, I'd change my program to discard undesired newline characters, unless they're truly invalid according to the input file format specification.
It is very easy to add additional newlines to the end of a file in every text editor. You have to push the cursor around to see them. Open your file in your editor and see what happens when you navigate to the end, you'll see the extra newlines.
There is no such thing as an EOF character in general. Windows treats control-Z as EOF in some cases. Perhaps you are talking about the return value from some API that indicates that it has reached the end of file?

Reading from a file

hello i got a problem with reading from a file, i am trying to read from a file using fscanf() and i cant seem to sort it out.
i try to read the file line by line and putting the string in a variable (buffer) each time but i cant understand how the while loop is suppose to be looking like
thanks in advance
the file that i want to read from is a txt file with this format: first line :"1234,abc,etc" second line : "2432,fjh,etc" and more lines like those i want to be able to use the fscanf method inorder to put in each loop the all line lets say "1234,abc,etc" in my string variable and so on till i dont have any more lines to read from
this is what i managed to gather so far (ofc its not the currect way to write it):
char* buffer[100];
while (fscanf(FILE *finput,"%s",buffer)!=something)
{
printf("%s",buffer);
}
i want this code to be able to print all of the lines in my code if you would be able to correct my errors i will greatly appriciate it
I feel like you should read some of these great topics first:
Trouble reading a line using fscanf()
Reading file using fscanf() in C
fscanf multiple lines [c++]
There are plenty of reasons why you should use fgets or something else instead.
Quoting from this place:
fscanf() is a field oriented function and is inappropriate for use in a robust, general-purpose text file reader. It has two major drawbacks:
You must know the exact data layout of the input file in advance and rewrite the function call for every different layout.
It's difficult to read text strings that contain spaces because fscanf() sees space characters as field delimiters.
If you know the size of file you're trying to read, you could use fread(), which is block oriented.

Resources