A more general approach to delimiters is the use of getc , which grabs one character at a time. Note that getc returns an int , so that we can test for equality with EOF. Use of an array avoids the need to use malloc and free to create a character pointer of the right length on the heap. You must free this duplicated string once you're done with it, or you'll get a memory leak:. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Going through a text file line by line in C Ask Question. Asked 9 years, 11 months ago. Active 3 years, 5 months ago. Viewed k times.
Improve this question. Dan Bradbury Dan Bradbury 1, 2 2 gold badges 20 20 silver badges 27 27 bronze badges. This code should not even compile. Note that while! Add a comment. The function reads the file correctly, and using printf I see that the constLine string did get read correctly as well.
If your task is not to invent the line-by-line reading function, but just to read the file line-by-line, you may use a typical code snippet involving the getline function see the manual page here :. In your readLine function, you return a pointer to the line array Strictly speaking, a pointer to its first character, but the difference is irrelevant here.
Since it's an automatic variable i. You see gibberish because printf has put its own stuff on the stack. You need to return a dynamically allocated buffer from the function. You already have one, it's lineBuffer ; all you have to do is truncate it to the desired length.
ADDED response to follow-up question in comment : readLine returns a pointer to the characters that make up the line. This pointer is what you need to work with the contents of the line.
It's also what you must pass to free when you've finished using the memory taken by these characters. Here's how you might use the readLine function:. A complete, fgets solution:. Remember, if you want to read from Standard Input rather than a file as in this case , then all you have to do is pass stdin as the third parameter of fgets method, like this:.
Removing trailing newline character from fgets input. Use fgets to read a line from a file handle. If the line is exactly characters long, count is at the point that gets executed. As others have pointed out, line is a locally declared array.
You can't return a pointer to it. You often see something like the following:. This is the way most of C libraries work. You should use the ANSI functions for reading a line, eg. After calling you need free in calling context, eg:. You make the mistake of returning a pointer to an automatic variable. The variable line is allocated in the stack and only lives as long as the function lives. You are not allowed to return a pointer to it, because as soon as it returns the memory will be given elsewhere.
To avoid this, you either return a pointer to memory which resides on the heap eg. Alternatively you can ask the user to pass you as an argument a memory address on which to write the line contents at.
I want a code from ground 0 so i did this to read the content of dictionary's word line by line. Note I've initialized the buffer With Null character each time I read line. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Reading a file line by line is a trivial problem in many programming languages, but not in C. The standard way of reading a line of text in C is to use the fgets function, which is fine if you know in advance how long a line of text could be.
You can find all the code examples and the input file at the GitHub repo for this article. This is a piece from the output of the above program on my machine:. The code prints the content of the chunk array, as filled after every call to fgets , and a marker string. If you watch carefully, by scrolling the above text snippet to the right, you can see that the output was truncated to characters per line of text.
This was expected because our code can store an entire line from the original text file only if the line can fit inside our chunk array. What if you need to have the entire line of text available for further processing and not a piece of line? As we've basically worked out in the comments: when there's already a thing, use the thing. To get a line from a file descriptor, you can. You've got a bit of a nightmare of lengths. You should be using explicit lengths for everything, and to heck with NUL termination.
Well, temp is of size And you should write sizeof temp instead of This means, if you read bytes, you write a null into the th byte in the buffer, and smash memory. A useless statement. And you should instead know how many bytes you have in temp. Then use memchr instead of strchr, memcpy instead of strcpy and strcat, etc This is trying to reproduce the prototype of read , but you forgot the buffer length altogether. This makes your function more comparable to gets then fgets.
Don't be like gets. Edit: One more thing. If you pass in an fd for a terminal, and it is in raw mode, you might get just a couple characters, not a complete line. This is sometimes known as a "short read". You need to keep reading until you get a line. For testing, this can be easily simulated by reading only a few bytes at a time.
0コメント