• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 08:16
CEST 14:16
KST 21:16
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
[ASL21] Ro24 Preview Pt2: News Flash9[ASL21] Ro24 Preview Pt1: New Chaos0Team Liquid Map Contest #22 - Presented by Monster Energy16ByuL: The Forgotten Master of ZvT30Behind the Blue - Team Liquid History Book20
Community News
Weekly Cups (March 23-29): herO takes triple6Aligulac acquired by REPLAYMAN.com/Stego Research8Weekly Cups (March 16-22): herO doubles, Cure surprises3Blizzard Classic Cup @ BlizzCon 2026 - $100k prize pool49Weekly Cups (March 9-15): herO, Clem, ByuN win4
StarCraft 2
General
What mix of new & old maps do you want in the next ladder pool? (SC2) Team Liquid Map Contest #22 - Presented by Monster Energy Aligulac acquired by REPLAYMAN.com/Stego Research Weekly Cups (March 23-29): herO takes triple herO wins SC2 All-Star Invitational
Tourneys
RSL Season 4 announced for March-April Sparkling Tuna Cup - Weekly Open Tournament StarCraft Evolution League (SC Evo Biweekly) WardiTV Mondays World University TeamLeague (500$+) | Signups Open
Strategy
Custom Maps
[M] (2) Frigid Storage Publishing has been re-enabled! [Feb 24th 2026]
External Content
Mutation # 519 Inner Power The PondCast: SC2 News & Results Mutation # 518 Radiation Zone Mutation # 517 Distant Threat
Brood War
General
Gypsy to Korea [ASL21] Ro24 Preview Pt2: News Flash ASL21 General Discussion How Can I Add Timer & APM Count? A cwal.gg Extension - Easily keep track of anyone
Tourneys
[ASL21] Ro24 Group F [ASL21] Ro24 Group E Azhi's Colosseum - Foreign KCM Escore Tournament StarCraft Season 2
Strategy
Fighting Spirit mining rates What's the deal with APM & what's its true value Simple Questions, Simple Answers
Other Games
General Games
Nintendo Switch Thread Stormgate/Frost Giant Megathread Starcraft Tabletop Miniature Game General RTS Discussion Thread Darkest Dungeon
Dota 2
The Story of Wings Gaming Official 'what is Dota anymore' discussion
League of Legends
G2 just beat GenG in First stand
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
Mafia Game Mode Feedback/Ideas TL Mafia Community Thread Five o'clock TL Mafia
Community
General
US Politics Mega-thread NASA and the Private Sector Russo-Ukrainian War Thread Things Aren’t Peaceful in Palestine Canadian Politics Mega-thread
Fan Clubs
The IdrA Fan Club
Media & Entertainment
[Manga] One Piece Movie Discussion! [Req][Books] Good Fantasy/SciFi books
Sports
2024 - 2026 Football Thread Formula 1 Discussion Cricket [SPORT] Tokyo Olympics 2021 Thread General nutrition recommendations
World Cup 2022
Tech Support
[G] How to Block Livestream Ads
TL Community
The Automated Ban List
Blogs
Funny Nicknames
LUCKY_NOOB
Money Laundering In Video Ga…
TrAiDoS
Iranian anarchists: organize…
XenOsky
FS++
Kraekkling
Shocked by a laser…
Spydermine0240
ASL S21 English Commentary…
namkraft
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1836 users

The Big Programming Thread - Page 673

Forum Index > General Forum
Post a Reply
Prev 1 671 672 673 674 675 1032 Next
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
Profile Blog Joined July 2014
3174 Posts
October 31 2015 14:21 GMT
#13441
Hi all, sorry for the noob question but it seems there is no already function in C to read line by line? Do I need to implement one by myself?

The one I found is "getLine". but that is not portable is it GNU function and not a standard.
Ropid
Profile Joined March 2009
Germany3557 Posts
October 31 2015 14:30 GMT
#13442
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?
"My goal is to replace my soul with coffee and become immortal."
Manit0u
Profile Blog Joined August 2004
Poland17708 Posts
October 31 2015 14:38 GMT
#13443
http://rosettacode.org/wiki/Read_a_file_line_by_line#C
https://www.gnu.org/software/libc/manual/html_node/Line-Input.html
Time is precious. Waste it wisely.
Wrath
Profile Blog Joined July 2014
3174 Posts
October 31 2015 14:51 GMT
#13444
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
Profile Blog Joined August 2010
United States13132 Posts
October 31 2015 14:53 GMT
#13445
On October 31 2015 23:51 WrathSCII wrote:
Show nested quote +
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.


Make a while loop on the condition that the char isn't carriage return/line feed.
I'll always be your shadow and veil your eyes from states of ain soph aur.
Wrath
Profile Blog Joined July 2014
3174 Posts
October 31 2015 15:08 GMT
#13446
On October 31 2015 23:53 Blitzkrieg0 wrote:
Show nested quote +
On October 31 2015 23:51 WrathSCII wrote:
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.


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...)


void read_line(FILE *fp)
{
char* buffer;
char temp;
buffer = (char*)calloc(100, sizeof(char));
char *p = buffer;
*p = (char)fgetc(fp);
while (*p != '\n')
{
p++;
*p = (char)fgetc(fp);
}
printf("%s", buffer);

}


This is how I use the function in the main:

int main()
{
FILE *fp = fopen("C:\\enable.txt", "r");
read_line(fp);
read_line(fp);

return 0;
}


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
Profile Blog Joined August 2010
United States13132 Posts
Last Edited: 2015-10-31 15:19:56
October 31 2015 15:15 GMT
#13447
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.
I'll always be your shadow and veil your eyes from states of ain soph aur.
Wrath
Profile Blog Joined July 2014
3174 Posts
October 31 2015 15:21 GMT
#13448
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
Profile Blog Joined August 2010
United States13132 Posts
Last Edited: 2015-10-31 22:29:10
October 31 2015 15:31 GMT
#13449
On November 01 2015 00:21 WrathSCII wrote:
Show nested quote +
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.


fgetc increments the file pointer.
I'll always be your shadow and veil your eyes from states of ain soph aur.
netherh
Profile Blog Joined November 2011
United Kingdom333 Posts
Last Edited: 2015-10-31 15:33:41
October 31 2015 15:32 GMT
#13450
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
Profile Blog Joined November 2011
United Kingdom333 Posts
Last Edited: 2015-10-31 15:33:18
October 31 2015 15:32 GMT
#13451
Oops
Ropid
Profile Joined March 2009
Germany3557 Posts
Last Edited: 2015-10-31 17:33:19
October 31 2015 17:32 GMT
#13452
On October 31 2015 23:51 WrathSCII wrote:
Show nested quote +
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.

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.
"My goal is to replace my soul with coffee and become immortal."
Manit0u
Profile Blog Joined August 2004
Poland17708 Posts
Last Edited: 2015-10-31 18:13:47
October 31 2015 18:03 GMT
#13453
Why can't you do it like that?

http://www.tutorialspoint.com/c_standard_library/c_function_fgets.htm


#include <stdio.h>

int main ()
{
FILE *handle;
char buffer[4096];

handle = fopen("file.txt","r");

if (handle == NULL)
{
perror("Error opening file");

return(-1);
}

while (fgets(buffer, 4096, handle) != NULL)
{
puts(buffer);
}

if (!feof(handle))
{
perror("Error: unexpected fgets() fail.");
}

fclose(handle);

return(0);
}


Something like that...
Time is precious. Waste it wisely.
Wrath
Profile Blog Joined July 2014
3174 Posts
October 31 2015 19:07 GMT
#13454
^ I really did not know that it is possible with fgets that it stops reading once it reaches the "\n"

Thanks!
WarSame
Profile Blog Joined February 2010
Canada1950 Posts
October 31 2015 21:42 GMT
#13455
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?
Can it be I stayed away too long? Did you miss these rhymes while I was gone?
Shield
Profile Blog Joined August 2009
Bulgaria4824 Posts
Last Edited: 2015-10-31 23:10:24
October 31 2015 22:25 GMT
#13456
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
Profile Joined March 2009
Germany3557 Posts
October 31 2015 22:46 GMT
#13457
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.
"My goal is to replace my soul with coffee and become immortal."
WarSame
Profile Blog Joined February 2010
Canada1950 Posts
October 31 2015 22:48 GMT
#13458
Thanks, I appreciate the response. Anacron is probably what I want.
Can it be I stayed away too long? Did you miss these rhymes while I was gone?
Manit0u
Profile Blog Joined August 2004
Poland17708 Posts
October 31 2015 23:50 GMT
#13459
On November 01 2015 07:25 darkness wrote:
Show nested quote +
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.


EOL, EOF is end of file
Time is precious. Waste it wisely.
phantomfive
Profile Joined April 2010
Korea (South)404 Posts
November 01 2015 04:03 GMT
#13460
On November 01 2015 07:46 Ropid wrote:
Another thing is your machine might use "systemd" as the init system..

Noooooooooooooooooo!!!
To ease another's heartache is to forget one's own - Lincoln
Prev 1 671 672 673 674 675 1032 Next
Please log in or register to reply.
Live Events Refresh
Next event in 6h 44m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
Lowko251
SortOf 189
trigger 84
Rex 83
Creator 34
StarCraft: Brood War
Britney 44184
Horang2 3034
Shuttle 1079
Mini 552
Last 218
BeSt 189
Soma 186
ggaemo 165
Rush 161
Soulkey 152
[ Show more ]
Mind 96
Dewaltoss 72
sSak 40
sorry 39
Movie 22
[sc1f]eonzerg 19
Shinee 17
ajuk12(nOOB) 14
IntoTheRainbow 14
GoRush 13
Noble 12
Bale 11
Shine 9
Dota 2
Gorgc3847
Counter-Strike
fl0m2131
Heroes of the Storm
MindelVK17
Other Games
gofns11078
B2W.Neo1444
ProTech119
Sick73
Mew2King72
QueenE45
Beastyqt40
Organizations
Counter-Strike
PGL17173
StarCraft 2
ComeBackTV 219
Other Games
BasetradeTV191
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 14 non-featured ]
StarCraft 2
• StrangeGG 53
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• Michael_bg 7
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
League of Legends
• Jankos2229
• Stunt602
Upcoming Events
BSL
6h 44m
RSL Revival
18h 44m
Cure vs Rogue
Maru vs TBD
MaxPax vs TBD
uThermal 2v2 Circuit
1d 1h
BSL
1d 6h
Afreeca Starleague
1d 21h
Wardi Open
1d 21h
Replay Cast
2 days
Sparkling Tuna Cup
2 days
Kung Fu Cup
3 days
The PondCast
4 days
[ Show More ]
Replay Cast
5 days
Replay Cast
6 days
CranKy Ducklings
6 days
Liquipedia Results

Completed

Escore Tournament S2: W1
WardiTV Winter 2026
NationLESS Cup

Ongoing

BSL Season 22
CSL Elite League 2026
ASL Season 21
CSL Season 20: Qualifier 2
StarCraft2 Community Team League 2026 Spring
RSL Revival: Season 4
Nations Cup 2026
PGL Bucharest 2026
Stake Ranked Episode 1
BLAST Open Spring 2026
ESL Pro League S23 Finals
ESL Pro League S23 Stage 1&2
PGL Cluj-Napoca 2026
IEM Kraków 2026
BLAST Bounty Winter 2026

Upcoming

CSL 2026 SPRING (S20)
IPSL Spring 2026
Acropolis #4
BSL 22 Non-Korean Championship
CSLAN 4
Kung Fu Cup 2026 Grand Finals
HSC XXIX
uThermal 2v2 2026 Main Event
IEM Cologne Major 2026
Stake Ranked Episode 2
CS Asia Championships 2026
Asian Champions League 2026
IEM Atlanta 2026
PGL Astana 2026
BLAST Rivals Spring 2026
CCT Season 3 Global Finals
IEM Rio 2026
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2026 TLnet. All Rights Reserved.