• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 21:37
CEST 03:37
KST 10:37
  • 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
Team TLMC #5 - Finalists & Open Tournaments0[ASL20] Ro16 Preview Pt2: Turbulence10Classic Games #3: Rogue vs Serral at BlizzCon9[ASL20] Ro16 Preview Pt1: Ascent10Maestros of the Game: Week 1/Play-in Preview12
Community News
StarCraft II 5.0.15 PTR Patch Notes60BSL 2025 Warsaw LAN + Legends Showmatch0Weekly Cups (Sept 8-14): herO & MaxPax split cups4WardiTV TL Team Map Contest #5 Tournaments1SC4ALL $6,000 Open LAN in Philadelphia8
StarCraft 2
General
StarCraft II 5.0.15 PTR Patch Notes #1: Maru - Greatest Players of All Time Weekly Cups (Sept 8-14): herO & MaxPax split cups Team Liquid Map Contest #21 - Presented by Monster Energy SpeCial on The Tasteless Podcast
Tourneys
SC2's Safe House 2 - October 18 & 19 RSL: Revival, a new crowdfunded tournament series Maestros of The Game—$20k event w/ live finals in Paris Sparkling Tuna Cup - Weekly Open Tournament SC4ALL $6,000 Open LAN in Philadelphia
Strategy
Custom Maps
External Content
Mutation # 491 Night Drive Mutation # 490 Masters of Midnight Mutation # 489 Bannable Offense Mutation # 488 What Goes Around
Brood War
General
Soulkey on ASL S20 ASL20 General Discussion BW General Discussion ASL TICKET LIVE help! :D NaDa's Body
Tourneys
[ASL20] Ro16 Group D [ASL20] Ro16 Group C Small VOD Thread 2.0 [Megathread] Daily Proleagues
Strategy
Simple Questions, Simple Answers Muta micro map competition Fighting Spirit mining rates [G] Mineral Boosting
Other Games
General Games
Stormgate/Frost Giant Megathread Borderlands 3 Path of Exile Nintendo Switch Thread General RTS Discussion Thread
Dota 2
Official 'what is Dota anymore' discussion LiquidDota to reintegrate into TL.net
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread
Community
General
UK Politics Mega-thread US Politics Mega-thread Things Aren’t Peaceful in Palestine Russo-Ukrainian War Thread Canadian Politics Mega-thread
Fan Clubs
The Happy Fan Club!
Media & Entertainment
Movie Discussion! [Manga] One Piece Anime Discussion Thread
Sports
2024 - 2026 Football Thread Formula 1 Discussion MLB/Baseball 2023
World Cup 2022
Tech Support
Linksys AE2500 USB WIFI keeps disconnecting Computer Build, Upgrade & Buying Resource Thread High temperatures on bridge(s)
TL Community
BarCraft in Tokyo Japan for ASL Season5 Final The Automated Ban List
Blogs
i'm really bored guys
Peanutsc
I <=> 9
KrillinFromwales
The Personality of a Spender…
TrAiDoS
A very expensive lesson on ma…
Garnet
hello world
radishsoup
Lemme tell you a thing o…
JoinTheRain
RTS Design in Hypercoven
a11
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1930 users

The Big Programming Thread - Page 673

Forum Index > General Forum
Post a Reply
Prev 1 671 672 673 674 675 1031 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
Poland17342 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
Poland17342 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
Poland17342 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 1031 Next
Please log in or register to reply.
Live Events Refresh
Next event in 8h 23m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
WinterStarcraft456
RuFF_SC2 120
CosmosSc2 66
Vindicta 22
StarCraft: Brood War
Aegong 1271
Artosis 692
Shuttle 514
Light 123
NaDa 20
ajuk12(nOOB) 11
Dota 2
monkeys_forever864
NeuroSwarm166
Counter-Strike
Stewie2K584
Fnx 387
PGG 72
Super Smash Bros
C9.Mang0373
Mew2King36
Other Games
summit1g7612
shahzam902
JimRising 628
Trikslyr55
ViBE40
Nina20
Organizations
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 14 non-featured ]
StarCraft 2
• davetesta26
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• RayReign 15
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
League of Legends
• Doublelift4671
Other Games
• Scarra1043
Upcoming Events
RSL Revival
8h 23m
Zoun vs Classic
Map Test Tournament
9h 23m
Korean StarCraft League
1d 1h
BSL Open LAN 2025 - War…
1d 6h
RSL Revival
1d 8h
Reynor vs Cure
BSL Open LAN 2025 - War…
2 days
RSL Revival
2 days
Online Event
2 days
Wardi Open
3 days
Monday Night Weeklies
3 days
[ Show More ]
Sparkling Tuna Cup
4 days
LiuLi Cup
5 days
The PondCast
6 days
Liquipedia Results

Completed

Proleague 2025-09-10
Chzzk MurlocKing SC1 vs SC2 Cup #2
HCC Europe

Ongoing

BSL 20 Team Wars
KCM Race Survival 2025 Season 3
BSL 21 Points
ASL Season 20
CSL 2025 AUTUMN (S18)
LASL Season 20
RSL Revival: Season 2
Maestros of the Game
StarSeries Fall 2025
FISSURE Playground #2
BLAST Open Fall 2025
BLAST Open Fall Qual
Esports World Cup 2025
BLAST Bounty Fall 2025
BLAST Bounty Fall Qual
IEM Cologne 2025
FISSURE Playground #1

Upcoming

2025 Chongqing Offline CUP
BSL World Championship of Poland 2025
IPSL Winter 2025-26
BSL Season 21
SC4ALL: Brood War
BSL 21 Team A
Stellar Fest
SC4ALL: StarCraft II
EC S1
ESL Impact League Season 8
SL Budapest Major 2025
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
Thunderpick World Champ.
CS Asia Championships 2025
ESL Pro League S22
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.