• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 18:44
CET 00:44
KST 08:44
  • 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
RSL Revival - 2025 Season Finals Preview8RSL Season 3 - Playoffs Preview0RSL Season 3 - RO16 Groups C & D Preview0RSL Season 3 - RO16 Groups A & B Preview2TL.net Map Contest #21: Winners12
Community News
ComeBackTV's documentary on Byun's Career !8Weekly Cups (Dec 8-14): MaxPax, Clem, Cure win4Weekly Cups (Dec 1-7): Clem doubles, Solar gets over the hump1Weekly Cups (Nov 24-30): MaxPax, Clem, herO win2BGE Stara Zagora 2026 announced15
StarCraft 2
General
When will we find out if there are more tournament ComeBackTV's documentary on Byun's Career ! Weekly Cups (Dec 8-14): MaxPax, Clem, Cure win RSL Revival - 2025 Season Finals Preview Weekly Cups (Dec 1-7): Clem doubles, Solar gets over the hump
Tourneys
Sparkling Tuna Cup - Weekly Open Tournament $5,000+ WardiTV 2025 Championship RSL Offline Finals Info - Dec 13 and 14! Master Swan Open (Global Bronze-Master 2) Winter Warp Gate Amateur Showdown #1:
Strategy
Custom Maps
Map Editor closed ?
External Content
Mutation # 504 Retribution Mutation # 503 Fowl Play Mutation # 502 Negative Reinforcement Mutation # 501 Price of Progress
Brood War
General
How Rain Became ProGamer in Just 3 Months BGH Auto Balance -> http://bghmmr.eu/ FlaSh on: Biggest Problem With SnOw's Playstyle screp: Command line app to parse SC rep files [BSL21] RO8 Bracket & Prediction Contest
Tourneys
Small VOD Thread 2.0 [Megathread] Daily Proleagues [BSL21] WB SEMIFINALS - Saturday 21:00 CET [BSL21] RO8 - Day 2 - Sunday 21:00 CET
Strategy
Game Theory for Starcraft Current Meta Simple Questions, Simple Answers Fighting Spirit mining rates
Other Games
General Games
Mechabellum Stormgate/Frost Giant Megathread Nintendo Switch Thread PC Games Sales Thread Path of Exile
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 Survivor II: The Amazon Sengoku Mafia TL Mafia Community Thread
Community
General
US Politics Mega-thread Russo-Ukrainian War Thread Things Aren’t Peaceful in Palestine The Games Industry And ATVI YouTube Thread
Fan Clubs
White-Ra Fan Club
Media & Entertainment
Anime Discussion Thread [Manga] One Piece Movie Discussion!
Sports
2024 - 2026 Football Thread Formula 1 Discussion
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
TL+ Announced Where to ask questions and add stream?
Blogs
The (Hidden) Drug Problem in…
TrAiDoS
I decided to write a webnov…
DjKniteX
James Bond movies ranking - pa…
Topin
Thanks for the RSL
Hildegard
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1976 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
Poland17539 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
Poland17539 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
Poland17539 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
The PiG Daily
21:00
Best Games of Starcraft
SHIN vs ByuN
Reynor vs Classic
TBD vs herO
Maru vs SHIN
TBD vs Classic
PiGStarcraft676
LiquipediaDiscussion
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
PiGStarcraft676
SpeCial 5
StarCraft: Brood War
Shuttle 116
Mong 16
NaDa 9
Dota 2
PGG 96
LuMiX0
Counter-Strike
Foxcn178
Other Games
Grubby6532
FrodaN1201
Liquid`Hasu238
C9.Mang0170
taco 96
B2W.Neo95
Liquid`Ken53
Trikslyr40
ZombieGrub31
PPMD29
Organizations
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 19 non-featured ]
StarCraft 2
• Hupsaiya 77
• RyuSc2 68
• davetesta62
• musti20045 38
• Kozan
• LaughNgamezSOOP
• sooper7s
• AfreecaTV YouTube
• intothetv
• Migwel
• IndyKCrew
StarCraft: Brood War
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• masondota22282
• WagamamaTV1104
League of Legends
• Doublelift3976
Other Games
• imaqtpie1741
• Scarra733
Upcoming Events
CranKy Ducklings
10h 16m
WardiTV 2025
11h 16m
Spirit vs Cure
Reynor vs MaxPax
SHIN vs TBD
Solar vs herO
Classic vs TBD
SC Evo League
12h 46m
Ladder Legends
19h 16m
BSL 21
20h 16m
Sziky vs Dewalt
eOnzErG vs Cross
Sparkling Tuna Cup
1d 10h
Ladder Legends
1d 17h
BSL 21
1d 20h
StRyKeR vs TBD
Bonyth vs TBD
Replay Cast
2 days
Wardi Open
2 days
[ Show More ]
Monday Night Weeklies
2 days
WardiTV Invitational
4 days
Replay Cast
5 days
WardiTV Invitational
5 days
ByuN vs Solar
Clem vs Classic
Cure vs herO
Reynor vs MaxPax
Liquipedia Results

Completed

Acropolis #4 - TS3
RSL Offline Finals
Kuram Kup

Ongoing

C-Race Season 1
IPSL Winter 2025-26
KCM Race Survival 2025 Season 4
YSL S2
BSL Season 21
Slon Tour Season 2
CSL Season 19: Qualifier 1
WardiTV 2025
META Madness #9
eXTREMESLAND 2025
SL Budapest Major 2025
ESL Impact League Season 8
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
Thunderpick World Champ.
CS Asia Championships 2025
ESL Pro League S22

Upcoming

CSL Season 19: Qualifier 2
CSL 2025 WINTER (S19)
BSL 21 Non-Korean Championship
Acropolis #4
IPSL Spring 2026
Bellum Gens Elite Stara Zagora 2026
HSC XXVIII
Big Gabe Cup #3
OSC Championship Season 13
ESL Pro League Season 23
PGL Cluj-Napoca 2026
IEM Kraków 2026
BLAST Bounty Winter 2026
BLAST Bounty Winter Qual
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 © 2025 TLnet. All Rights Reserved.