Is there a fopen-like function in elisp












3















I am aware of insert-file-contents idiom:



(defun read-lines (filePath)
(with-temp-buffer
(insert-file-contents filePath)
(split-string (buffer-string) "n" t)))


But is there a more natural way to read a file line by line without reading the whole file at once? I am looking for fopen/fread-like functionality.










share|improve this question

























  • I'll just note that you can pass BEG and END parameters to insert-file-contents to insert only part of the file. Not what you're asking for, but worth pointing out.

    – phils
    Oct 16 '18 at 23:22


















3















I am aware of insert-file-contents idiom:



(defun read-lines (filePath)
(with-temp-buffer
(insert-file-contents filePath)
(split-string (buffer-string) "n" t)))


But is there a more natural way to read a file line by line without reading the whole file at once? I am looking for fopen/fread-like functionality.










share|improve this question

























  • I'll just note that you can pass BEG and END parameters to insert-file-contents to insert only part of the file. Not what you're asking for, but worth pointing out.

    – phils
    Oct 16 '18 at 23:22
















3












3








3


1






I am aware of insert-file-contents idiom:



(defun read-lines (filePath)
(with-temp-buffer
(insert-file-contents filePath)
(split-string (buffer-string) "n" t)))


But is there a more natural way to read a file line by line without reading the whole file at once? I am looking for fopen/fread-like functionality.










share|improve this question
















I am aware of insert-file-contents idiom:



(defun read-lines (filePath)
(with-temp-buffer
(insert-file-contents filePath)
(split-string (buffer-string) "n" t)))


But is there a more natural way to read a file line by line without reading the whole file at once? I am looking for fopen/fread-like functionality.







emacs elisp






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 9:45









tripleee

94.2k13132186




94.2k13132186










asked Oct 14 '18 at 13:11









ivaigultivaigult

3,12021841




3,12021841













  • I'll just note that you can pass BEG and END parameters to insert-file-contents to insert only part of the file. Not what you're asking for, but worth pointing out.

    – phils
    Oct 16 '18 at 23:22





















  • I'll just note that you can pass BEG and END parameters to insert-file-contents to insert only part of the file. Not what you're asking for, but worth pointing out.

    – phils
    Oct 16 '18 at 23:22



















I'll just note that you can pass BEG and END parameters to insert-file-contents to insert only part of the file. Not what you're asking for, but worth pointing out.

– phils
Oct 16 '18 at 23:22







I'll just note that you can pass BEG and END parameters to insert-file-contents to insert only part of the file. Not what you're asking for, but worth pointing out.

– phils
Oct 16 '18 at 23:22














1 Answer
1






active

oldest

votes


















0














I think that the natural way of working with files in emacs is loading the file in buffer, After that you can process it line by line. take also a look to this answer in emacs stackexchange and this blog post from ergoemacs



like for example this:



ELISP> (find-file "foo.txt")
#<buffer foo.txt>
ELISP> (goto-char 1)
1 (#o1, #x1, ?C-a)
ELISP> (while (not (eobp))
(print (current-line-contents))
(forward-line 1))


Sample emacs interaction



In order not to get the properties, you can use the function thing-at point:



ELISP> (goto-char 1)
1 (#o1, #x1, ?C-a)
ELISP> (while (not (eobp))
(print (thing-at-point 'line t))
(forward-line 1))

"line 1
"

"line 2
"

"line 3
"

"line 4
"

nil


if you need to work with temporary bufferss for speed reasons try this:



 (with-temp-buffer
(insert-file-contents "./foo.txt")
(while (not (eobp))
(print (thing-at-point 'line t))
(forward-line 1)))





share|improve this answer


























  • My problem with copying a file to a buffer is that it is horribly slow. It could be like five minutes for non-minified JSON file. Especially, it is slow when the file is a binary file or doesn't contain line endings.

    – ivaigult
    Nov 16 '18 at 9:58











  • I updated the question maybe it shloud work for you, now in terms of speed

    – anquegi
    Nov 16 '18 at 10:04













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52803006%2fis-there-a-fopen-like-function-in-elisp%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














I think that the natural way of working with files in emacs is loading the file in buffer, After that you can process it line by line. take also a look to this answer in emacs stackexchange and this blog post from ergoemacs



like for example this:



ELISP> (find-file "foo.txt")
#<buffer foo.txt>
ELISP> (goto-char 1)
1 (#o1, #x1, ?C-a)
ELISP> (while (not (eobp))
(print (current-line-contents))
(forward-line 1))


Sample emacs interaction



In order not to get the properties, you can use the function thing-at point:



ELISP> (goto-char 1)
1 (#o1, #x1, ?C-a)
ELISP> (while (not (eobp))
(print (thing-at-point 'line t))
(forward-line 1))

"line 1
"

"line 2
"

"line 3
"

"line 4
"

nil


if you need to work with temporary bufferss for speed reasons try this:



 (with-temp-buffer
(insert-file-contents "./foo.txt")
(while (not (eobp))
(print (thing-at-point 'line t))
(forward-line 1)))





share|improve this answer


























  • My problem with copying a file to a buffer is that it is horribly slow. It could be like five minutes for non-minified JSON file. Especially, it is slow when the file is a binary file or doesn't contain line endings.

    – ivaigult
    Nov 16 '18 at 9:58











  • I updated the question maybe it shloud work for you, now in terms of speed

    – anquegi
    Nov 16 '18 at 10:04


















0














I think that the natural way of working with files in emacs is loading the file in buffer, After that you can process it line by line. take also a look to this answer in emacs stackexchange and this blog post from ergoemacs



like for example this:



ELISP> (find-file "foo.txt")
#<buffer foo.txt>
ELISP> (goto-char 1)
1 (#o1, #x1, ?C-a)
ELISP> (while (not (eobp))
(print (current-line-contents))
(forward-line 1))


Sample emacs interaction



In order not to get the properties, you can use the function thing-at point:



ELISP> (goto-char 1)
1 (#o1, #x1, ?C-a)
ELISP> (while (not (eobp))
(print (thing-at-point 'line t))
(forward-line 1))

"line 1
"

"line 2
"

"line 3
"

"line 4
"

nil


if you need to work with temporary bufferss for speed reasons try this:



 (with-temp-buffer
(insert-file-contents "./foo.txt")
(while (not (eobp))
(print (thing-at-point 'line t))
(forward-line 1)))





share|improve this answer


























  • My problem with copying a file to a buffer is that it is horribly slow. It could be like five minutes for non-minified JSON file. Especially, it is slow when the file is a binary file or doesn't contain line endings.

    – ivaigult
    Nov 16 '18 at 9:58











  • I updated the question maybe it shloud work for you, now in terms of speed

    – anquegi
    Nov 16 '18 at 10:04
















0












0








0







I think that the natural way of working with files in emacs is loading the file in buffer, After that you can process it line by line. take also a look to this answer in emacs stackexchange and this blog post from ergoemacs



like for example this:



ELISP> (find-file "foo.txt")
#<buffer foo.txt>
ELISP> (goto-char 1)
1 (#o1, #x1, ?C-a)
ELISP> (while (not (eobp))
(print (current-line-contents))
(forward-line 1))


Sample emacs interaction



In order not to get the properties, you can use the function thing-at point:



ELISP> (goto-char 1)
1 (#o1, #x1, ?C-a)
ELISP> (while (not (eobp))
(print (thing-at-point 'line t))
(forward-line 1))

"line 1
"

"line 2
"

"line 3
"

"line 4
"

nil


if you need to work with temporary bufferss for speed reasons try this:



 (with-temp-buffer
(insert-file-contents "./foo.txt")
(while (not (eobp))
(print (thing-at-point 'line t))
(forward-line 1)))





share|improve this answer















I think that the natural way of working with files in emacs is loading the file in buffer, After that you can process it line by line. take also a look to this answer in emacs stackexchange and this blog post from ergoemacs



like for example this:



ELISP> (find-file "foo.txt")
#<buffer foo.txt>
ELISP> (goto-char 1)
1 (#o1, #x1, ?C-a)
ELISP> (while (not (eobp))
(print (current-line-contents))
(forward-line 1))


Sample emacs interaction



In order not to get the properties, you can use the function thing-at point:



ELISP> (goto-char 1)
1 (#o1, #x1, ?C-a)
ELISP> (while (not (eobp))
(print (thing-at-point 'line t))
(forward-line 1))

"line 1
"

"line 2
"

"line 3
"

"line 4
"

nil


if you need to work with temporary bufferss for speed reasons try this:



 (with-temp-buffer
(insert-file-contents "./foo.txt")
(while (not (eobp))
(print (thing-at-point 'line t))
(forward-line 1)))






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 10:07

























answered Nov 15 '18 at 19:31









anquegianquegi

6,38432845




6,38432845













  • My problem with copying a file to a buffer is that it is horribly slow. It could be like five minutes for non-minified JSON file. Especially, it is slow when the file is a binary file or doesn't contain line endings.

    – ivaigult
    Nov 16 '18 at 9:58











  • I updated the question maybe it shloud work for you, now in terms of speed

    – anquegi
    Nov 16 '18 at 10:04





















  • My problem with copying a file to a buffer is that it is horribly slow. It could be like five minutes for non-minified JSON file. Especially, it is slow when the file is a binary file or doesn't contain line endings.

    – ivaigult
    Nov 16 '18 at 9:58











  • I updated the question maybe it shloud work for you, now in terms of speed

    – anquegi
    Nov 16 '18 at 10:04



















My problem with copying a file to a buffer is that it is horribly slow. It could be like five minutes for non-minified JSON file. Especially, it is slow when the file is a binary file or doesn't contain line endings.

– ivaigult
Nov 16 '18 at 9:58





My problem with copying a file to a buffer is that it is horribly slow. It could be like five minutes for non-minified JSON file. Especially, it is slow when the file is a binary file or doesn't contain line endings.

– ivaigult
Nov 16 '18 at 9:58













I updated the question maybe it shloud work for you, now in terms of speed

– anquegi
Nov 16 '18 at 10:04







I updated the question maybe it shloud work for you, now in terms of speed

– anquegi
Nov 16 '18 at 10:04






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52803006%2fis-there-a-fopen-like-function-in-elisp%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python