How to read lines from a file (.txt, .asm) - c

Like my input would be stored in a .txt/.asm file, and the input is kind of like this:
MOV Ri,Rj
MVI Ri,X
LOAD Ri,X(Rj)
STORE Ri,X(Rj)
So, I need to read them line by line and also store these. What is the best way to do this? Like I have checked fscanf. But it seems like it will handle cases best when there is data of fixed format, like (Hi, 2) (Bye, 3) (Hello, 4). But how should I go about handling my case? It should be able to read the entire line and then stop after the input finishes.
I apologize in advance if something like this has been asked earlier, but I couldn't find a question which matched what I asked.

Related

Writing multiline text files in Lua

I would like to know the best way to make my script write something into a file (lets say text.txt) in a way that would always add a line break at the end. When I append text using
file = io.open("test.txt", "a")
file:write("hello")
twice, the file looks like:
hellohello
But I want it to look like:
hello
hello
Unlike print, the new line character isn't added automatically when calling io.write, you can add it yourself:
file:write("hello", "\n")
The easiest way to achieve this would be to include a Newline character sequence every time you call the write method like so: file:write("hello\n") or so: file:write("hello", "\n"). This way, a script such as
file = io.open(test.txt, "a")
file:write("hello", "\n")
file:write("hello", "\n")
would result in the desired output:
hello
hello
There are, however, many other solutions to this (some being more elegant than the others). When outputting text in Java, for example, there are special methods such as BufferedWriter#newLine(), which will do the same thing in a more cleaner fashion. So if your interested in a different way of achieving this, I suggest you read up on the Lua docs for analogous methods/solutions.

How to write couple of lines into a file in C?

I'm having a variable called username and another one called password...
I need to register users into a system, and write it on a file..
My problem is, that the 2nd time or more, it writes the details on the same line..
For example :
in the first time I write for the file, it become something like this :
Amit 123456
but on the second time I'm registering a user, it doesnt go to the next line and keep writing from that spot:
Amit 123456Yosi 125818
where I need the file to be like this :
Amit 123456
Yosi 125818
I mean, I need to make the 2nd or more fprintf to the file go to the next line, and not to start from the specific place...
I've wrote this in the code :
fprintf(file, "%s %s\n", username, password);
though, the '\n'doesnt seems to make the next fprintf go to the next line...
In summery - I need to print to the file every time, on a new line..
on the first print to the file, it'll write on line 1
then on the second print to the file, it'll write on line 2, and not from the continue of line1...
Thanks :)
Had the same thought as the commenters: You may need a \r in there too, depending on your development environment. It could even be that the file viewer you are using is the issue (eg: Notepad.exe on Windows often has trouble with simple line endings)
I'm guessing you're on Windows, you need /r/n
Note that it is very, very bad practice to save passwords, please google how to work with passwords.

How to find word from the end of file in Lua

Ok I use method from here: How to Read only the last line of a text file in Lua?
The problem is that sometimes line can be bigger.
The question is how can i find first word "foo" from the end of file and then use everything after it?
The problem is that sometimes line can be bigger.
Then you just need to seek further back from the end.
The question is how can i find first word "foo" from the end of file and then use everything after it?
Grab a big enough chunk of the file to be sure you've got the last foo, the use .*foo to skip everything up to and including the last "foo" (.* is greedy).
local f = io.open('filename', 'r')
f:seek('end', -1024)
local text = f:read('*a')
local after = string.match(text, ".*foo(.*)")
f:close()
If the file is not too big and you're ready to take the easy way out this might help:
fh=io.open('myfile.txt','rb')
str=fh:read'*a'
pat='foo'
afterFoo=str:match('.*'..pat..'(.*)$')
fh:close()
If you need a more complex, but faster (in run time on large files) solution, my guess would be that you' read in the file in chunks, reverse each of them, and look for your pattern in reverse. Don't forget to look for your pattern across the borders (the chunks must overlap at least the length of the pattern you're seeking in the general case).
For more explanation about the block reading, see my post here.

Which method to use in searching for a line in a file

I have a file with path names to files:
/my/path1
/my/path11
/my/path12
/my/path13
The file structure is that it has individual paths in each line. All I want to do is search for the existence of a string /my/path1 or anyother in the above file many times
I could think of 2 methods.
every time get file contents line by line and then search the string. Advantage is that the file can be of anysize and I dont need to worry about buffer overflow.
Load the contents into a buffer and search it using the buffer. But as I dont have control over the file size I should be cautious here.
What is the best approach? I am working in unix. Is there any in-build library commands in C that I can make use of for this purpose? Or how can I accomplish the same task using awk in C code.
If you use stdio it will do the buffering for you. You can change its operation by using the function setvbuf to buffer more than a single line. getline can by used to check line by line.
I think loading all the file in memory is not a good idea. Using fgets and strcmp is the best way, I guess.

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