• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 09:50
CET 15:50
KST 23:50
  • 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
Behind the Blue - Team Liquid History Book15Clem wins HomeStory Cup 289HomeStory Cup 28 - Info & Preview13Rongyi Cup S3 - Preview & Info8herO wins SC2 All-Star Invitational14
Community News
ACS replaced by "ASL Season Open" - Starts 21/0218LiuLi Cup: 2025 Grand Finals (Feb 10-16)24Weekly Cups (Feb 2-8): Classic, Solar, MaxPax win2Nexon's StarCraft game could be FPS, led by UMS maker10PIG STY FESTIVAL 7.0! (19 Feb - 1 Mar)13
StarCraft 2
General
How do you think the 5.0.15 balance patch (Oct 2025) for StarCraft II has affected the game? Nexon's StarCraft game could be FPS, led by UMS maker Terran Scanner Sweep Behind the Blue - Team Liquid History Book Weekly Cups (Jan 12-18): herO, MaxPax, Solar win
Tourneys
LiuLi Cup: 2025 Grand Finals (Feb 10-16) RSL Season 4 announced for March-April PIG STY FESTIVAL 7.0! (19 Feb - 1 Mar) RSL Revival: Season 4 Korea Qualifier (Feb 14) Sparkling Tuna Cup - Weekly Open Tournament
Strategy
Custom Maps
Map Editor closed ? [A] Starcraft Sound Mod
External Content
The PondCast: SC2 News & Results Mutation # 512 Overclocked Mutation # 511 Temple of Rebirth Mutation # 510 Safety Violation
Brood War
General
Which units you wish saw more use in the game? StarCraft player reflex TE scores [ASL21] Potential Map Candidates Gypsy to Korea ACS replaced by "ASL Season Open" - Starts 21/02
Tourneys
Escore Tournament StarCraft Season 1 [Megathread] Daily Proleagues Small VOD Thread 2.0 KCM Race Survival 2026 Season 1
Strategy
Fighting Spirit mining rates Zealot bombing is no longer popular? Simple Questions, Simple Answers Current Meta
Other Games
General Games
Path of Exile Diablo 2 thread Nintendo Switch Thread Battle Aces/David Kim RTS Megathread ZeroSpace Megathread
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
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 Vanilla Mini Mafia TL Mafia Community Thread
Community
General
US Politics Mega-thread Ask and answer stupid questions here! European Politico-economics QA Mega-thread The Games Industry And ATVI Russo-Ukrainian War Thread
Fan Clubs
The IdrA Fan Club The herO Fan Club!
Media & Entertainment
[Req][Books] Good Fantasy/SciFi books [Manga] One Piece Anime Discussion Thread
Sports
2024 - 2026 Football Thread
World Cup 2022
Tech Support
TL Community
The Automated Ban List
Blogs
ADHD And Gaming Addiction…
TrAiDoS
My 2025 Magic: The Gathering…
DARKING
Life Update and thoughts.
FuDDx
How do archons sleep?
8882
StarCraft improvement
iopq
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1736 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
Poland17661 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
Poland17661 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
Poland17661 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
uThermal 2v2 Circuit
12:00
Bonus Cup #3
uThermal477
Liquipedia
LiuLi Cup
11:00
2025 Grand Finals Playoffs
Rogue vs TriGGeRLIVE!
ByuN vs Serral
RotterdaM1834
ComeBackTV 1135
PiGStarcraft639
IndyStarCraft 433
BRAT_OK 190
Rex166
3DClanTV 80
IntoTheiNu 22
Liquipedia
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
RotterdaM 1834
PiGStarcraft639
uThermal 477
IndyStarCraft 433
BRAT_OK 190
Rex 166
StarCraft: Brood War
Britney 42439
Flash 2147
Jaedong 1834
Hyuk 1145
firebathero 1079
Soma 540
Stork 408
Light 349
Snow 301
Soulkey 195
[ Show more ]
Rush 131
Last 110
Sea.KH 88
Bonyth 74
Movie 66
Pusan 65
Leta 64
Hm[arnc] 59
Aegong 49
Yoon 36
ToSsGirL 34
soO 32
sorry 31
Free 30
IntoTheRainbow 21
910 19
Terrorterran 18
JulyZerg 16
HiyA 16
Shine 15
ivOry 14
Shinee 13
Rock 12
yabsab 12
Dota 2
Gorgc5980
singsing3319
XcaliburYe146
febbydoto8
Counter-Strike
fl0m3425
shoxiejesuss1814
Super Smash Bros
Mew2King84
Heroes of the Storm
Khaldor387
Other Games
B2W.Neo976
DeMusliM501
crisheroes420
byalli266
Sick252
Hui .107
ArmadaUGS57
Organizations
Counter-Strike
PGL20294
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 15 non-featured ]
StarCraft 2
• Berry_CruncH115
• Adnapsc2 6
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• FirePhoenix5
• Michael_bg 2
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
League of Legends
• Jankos3379
Upcoming Events
PSISTORM Gaming Misc
2h 5m
RSL Revival
3h 10m
AI Arena Tournament
5h 10m
Replay Cast
9h 10m
Sparkling Tuna Cup
19h 10m
LiuLi Cup
20h 10m
Ladder Legends
1d 3h
Replay Cast
1d 9h
Replay Cast
1d 18h
Wardi Open
1d 21h
[ Show More ]
Monday Night Weeklies
2 days
OSC
2 days
WardiTV Winter Champion…
2 days
Replay Cast
3 days
WardiTV Winter Champion…
3 days
Replay Cast
4 days
PiG Sty Festival
4 days
The PondCast
4 days
KCM Race Survival
4 days
WardiTV Winter Champion…
4 days
Replay Cast
5 days
PiG Sty Festival
5 days
Epic.LAN
5 days
Replay Cast
6 days
PiG Sty Festival
6 days
CranKy Ducklings
6 days
Epic.LAN
6 days
Liquipedia Results

Completed

Escore Tournament S1: W8
Rongyi Cup S3
Underdog Cup #3

Ongoing

KCM Race Survival 2026 Season 1
LiuLi Cup: 2025 Grand Finals
Nations Cup 2026
PGL Cluj-Napoca 2026
IEM Kraków 2026
BLAST Bounty Winter 2026
BLAST Bounty Winter Qual
eXTREMESLAND 2025
SL Budapest Major 2025

Upcoming

Escore Tournament S1: King of Kings
[S:21] ASL SEASON OPEN 1st Round
[S:21] ASL SEASON OPEN 1st Round Qualifier
[S:21] ASL SEASON OPEN 2nd Round
[S:21] ASL SEASON OPEN 2nd Round Qualifier
Acropolis #4
IPSL Spring 2026
HSC XXIX
uThermal 2v2 2026 Main Event
Bellum Gens Elite Stara Zagora 2026
RSL Revival: Season 4
WardiTV Winter 2026
BLAST Rivals Spring 2026
CCT Season 3 Global Finals
FISSURE Playground #3
IEM Rio 2026
PGL Bucharest 2026
Stake Ranked Episode 1
BLAST Open Spring 2026
ESL Pro League Season 23
ESL Pro League Season 23
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.