The one I found is "getLine". but that is not portable is it GNU function and not a standard.
The Big Programming Thread - Page 673
Forum Index > General Forum |
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. | ||
Wrath
3174 Posts
The one I found is "getLine". but that is not portable is it GNU function and not a standard. | ||
Ropid
Germany3557 Posts
The man-page of getline() says it was added to POSIX at some point, so maybe Microsoft does also have it. Did you check? | ||
Manit0u
Poland17187 Posts
https://www.gnu.org/software/libc/manual/html_node/Line-Input.html | ||
Wrath
3174 Posts
On October 31 2015 23:30 Ropid wrote: There's fgets(). The man-page of getline() says it was added to POSIX at some point, so maybe Microsoft does also have it. Did you check? I checked on VS and it did not identify it. And I can't find on google that microsoft included it. Beside, the fgets() won't hellp much because it will read a defined number of characters instead of reading the whole line. If I have a file with different lines length, it won't help me. | ||
Blitzkrieg0
United States13132 Posts
On October 31 2015 23:51 WrathSCII wrote: I checked on VS and it did not identify it. And I can't find on google that microsoft included it. Beside, the fgets() won't hellp much because it will read a defined number of characters instead of reading the whole line. If I have a file with different lines length, it won't help me. Make a while loop on the condition that the char isn't carriage return/line feed. | ||
Wrath
3174 Posts
On October 31 2015 23:53 Blitzkrieg0 wrote: Make a while loop on the condition that the char isn't carriage return/line feed. I did that and here is my function that reads line by line (It is not perfect but it is doing the job... so far...)
This is how I use the function in the main:
I thought I'm doing pass by value here. so it should print the first line twice. But it is printing the 1st time it prints the 1st line and the 2nd time it prints the 2nd line. Any thoughts on why this is happening? | ||
Blitzkrieg0
United States13132 Posts
Maybe nitpicky, but you should be giving that memory back to the operating system at the end of the function as well. | ||
Wrath
3174 Posts
On November 01 2015 00:15 Blitzkrieg0 wrote: You're creating the buffer when you enter the function so a new buffer is created for the second line. If you want a single buffer you should create it in main and pass it to the function. Maybe nitpicky, but you should be giving that memory back to the operating system at the end of the function as well. Not sure how creating the buffer is related to my question here. I mean I pass the file pointer by value when I call it in the main. So the second I time I call it, the file pointer should not have changed in the first call as what was passed there is a copy of it. So theoretically it should once again print the first line. | ||
Blitzkrieg0
United States13132 Posts
On November 01 2015 00:21 WrathSCII wrote: Not sure how creating the buffer is related to my question here. I mean I pass the file pointer by value when I call it in the main. So the second I time I call it, the file pointer should not have changed in the first call as what was passed there is a copy of it. So theoretically it should once again print the first line. fgetc increments the file pointer. | ||
netherh
United Kingdom333 Posts
On November 01 2015 00:21 WrathSCII wrote: Not sure how creating the buffer is related to my question here. I mean I pass the file pointer by value when I call it in the main. So the second I time I call it, the file pointer should not have changed in the first call as what was passed there is a copy of it. So theoretically it should once again print the first line. That's not how it works: http://en.cppreference.com/w/c/io While you pass the file pointer by value (so the value of the pointer in main is the same afterwards), there's nothing to stop fgetc from changing the underlying object that the pointer points to. The file pointer isn't really a "pointer to a file" anyway. It represents a file input stream, that contains a current position in the file. When you call fgetc you read a character from the stream, and increment the position. So the next thing you read from the file continues from that point. | ||
netherh
United Kingdom333 Posts
| ||
Ropid
Germany3557 Posts
On October 31 2015 23:51 WrathSCII wrote: I checked on VS and it did not identify it. And I can't find on google that microsoft included it. Beside, the fgets() won't hellp much because it will read a defined number of characters instead of reading the whole line. If I have a file with different lines length, it won't help me. No, fgets() will stop after it finds a new-line. Its size parameter is a limit where you can tell fgets() the size of your buffer so that it won't overwrite your stuff. | ||
Manit0u
Poland17187 Posts
http://www.tutorialspoint.com/c_standard_library/c_function_fgets.htm
Something like that... | ||
Wrath
3174 Posts
Thanks! | ||
WarSame
Canada1950 Posts
| ||
Shield
Bulgaria4824 Posts
On November 01 2015 04:07 WrathSCII wrote: ^ I really did not know that it is possible with fgets that it stops reading once it reaches the "\n" Thanks! Same applies to std::cin I think. ![]() Edit: Actually, it may be EOF not \n. | ||
Ropid
Germany3557 Posts
On November 01 2015 06:42 WarSame wrote: In regards to cron, I can't seem to find this anywhere. You should only use crontab when your job doesn't fit nicely into the folders, right? So if your job should run daily at midnight then you put it in cron.daily, but if it runs at midnight and 0600, then it should be in the crontab? Yup, that's how I understood it as well. If you need special rules, you use crontab. There's also a thingy named "anacron" which you might want to look at. It's what deals with what happens when the machine was off at the time when a rule would have been hit. Normal cron simply doesn't do anything about that and scripts might never run. I think the daily, weekly, etc. folders are looked at by that anacron tool, but I don't know what's up with things that are in crontab. Another thing is your machine might use "systemd" as the init system. That has "timers" that can be used as an alternative to cron+anacron. | ||
WarSame
Canada1950 Posts
| ||
Manit0u
Poland17187 Posts
On November 01 2015 07:25 darkness wrote: Same applies to std::cin I think. ![]() Edit: Actually, it may be EOF not \n. EOL, EOF is end of file ![]() | ||
phantomfive
Korea (South)404 Posts
On November 01 2015 07:46 Ropid wrote: Another thing is your machine might use "systemd" as the init system.. Noooooooooooooooooo!!! | ||
| ||