Is there a fopen-like function in elisp
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
add a comment |
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
I'll just note that you can pass BEG and END parameters toinsert-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
add a comment |
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
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
emacs elisp
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 toinsert-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
add a comment |
I'll just note that you can pass BEG and END parameters toinsert-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
add a comment |
1 Answer
1
active
oldest
votes
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))
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)))
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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))
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)))
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
add a comment |
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))
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)))
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
add a comment |
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))
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)))
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))
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)))
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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