• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 12:03
CEST 18:03
KST 01:03
  • 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
[ASL19] Finals Recap: Standing Tall9HomeStory Cup 27 - Info & Preview18Classic wins Code S Season 2 (2025)16Code S RO4 & Finals Preview: herO, Rogue, Classic, GuMiho0TL Team Map Contest #5: Presented by Monster Energy6
Community News
Flash Announces Hiatus From ASL55Weekly Cups (June 23-29): Reynor in world title form?13FEL Cracov 2025 (July 27) - $8000 live event18Esports World Cup 2025 - Final Player Roster16Weekly Cups (June 16-22): Clem strikes back1
StarCraft 2
General
TRUSTED USDT RECOVERY TECHY FORCE CYBER RETRIEVAL Statistics for vetoed/disliked maps The SCII GOAT: A statistical Evaluation Weekly Cups (June 23-29): Reynor in world title form? PiG Sty Festival #5: Playoffs Preview + Groups Recap
Tourneys
FEL Cracov 2025 (July 27) - $8000 live event RSL: Revival, a new crowdfunded tournament series Korean Starcraft League Week 77 Master Swan Open (Global Bronze-Master 2) [GSL 2025] Code S: Season 2 - Semi Finals & Finals
Strategy
How did i lose this ZvP, whats the proper response Simple Questions Simple Answers
Custom Maps
[UMS] Zillion Zerglings
External Content
Mutation # 480 Moths to the Flame Mutation # 479 Worn Out Welcome Mutation # 478 Instant Karma Mutation # 477 Slow and Steady
Brood War
General
Player “Jedi” cheat on CSL Replays question BW General Discussion Flash Announces Hiatus From ASL BGH Auto Balance -> http://bghmmr.eu/
Tourneys
[Megathread] Daily Proleagues [BSL20] Grand Finals - Sunday 20:00 CET Small VOD Thread 2.0 [BSL20] GosuLeague RO16 - Tue & Wed 20:00+CET
Strategy
Simple Questions, Simple Answers I am doing this better than progamers do.
Other Games
General Games
Stormgate/Frost Giant Megathread Nintendo Switch Thread Path of Exile What do you want from future RTS games? Beyond All Reason
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
Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread Vanilla Mini Mafia
Community
General
US Politics Mega-thread Trading/Investing Thread Things Aren’t Peaceful in Palestine Russo-Ukrainian War Thread The Games Industry And ATVI
Fan Clubs
SKT1 Classic Fan Club! Maru Fan Club
Media & Entertainment
Anime Discussion Thread [Manga] One Piece [\m/] Heavy Metal Thread
Sports
Formula 1 Discussion 2024 - 2025 Football Thread NBA General Discussion TeamLiquid Health and Fitness Initiative For 2023 NHL Playoffs 2024
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
Blogs
Culture Clash in Video Games…
TrAiDoS
from making sc maps to makin…
Husyelt
Blog #2
tankgirl
StarCraft improvement
iopq
Trip to the Zoo
micronesia
Customize Sidebar...

Website Feedback

Closed Threads



Active: 673 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
Poland17243 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
Poland17243 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
Poland17243 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
FEL
16:00
Cracov 2025: Qualifier #1
CranKy Ducklings31
Liquipedia
PSISTORM Gaming Misc
15:55
FSL team league: ASP vs PTB
Liquipedia
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
trigger 333
Hui .280
SC2Nice 23
StarCraft: Brood War
Calm 9558
Horang2 2731
Bisu 1917
Jaedong 1733
Flash 1466
Mini 975
Larva 967
firebathero 715
BeSt 582
actioN 383
[ Show more ]
hero 340
Mind 167
Hyun 139
sSak 55
GoRush 31
Mong 27
Rock 16
zelot 15
Dota 2
LuMiX3
Heroes of the Storm
Khaldor534
Other Games
Gorgc3903
singsing3045
B2W.Neo1311
FrodaN1120
Mlord344
Fuzer 337
Lowko307
TKL 164
KnowMe126
Trikslyr60
Organizations
Other Games
EGCTV1185
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 17 non-featured ]
StarCraft 2
• HeavenSC 27
• Kozan
• LaughNgamezSOOP
• AfreecaTV YouTube
• sooper7s
• Migwel
• intothetv
• IndyKCrew
StarCraft: Brood War
• Michael_bg 5
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• C_a_k_e 2956
• WagamamaTV624
• Ler58
League of Legends
• Nemesis8010
Other Games
• Shiphtur29
Upcoming Events
RSL Revival
17h 58m
Clem vs Classic
SHIN vs Cure
FEL
19h 58m
WardiTV European League
19h 58m
BSL: ProLeague
1d 1h
Dewalt vs Bonyth
Replay Cast
2 days
Sparkling Tuna Cup
2 days
WardiTV European League
2 days
The PondCast
3 days
Replay Cast
4 days
RSL Revival
4 days
[ Show More ]
Replay Cast
5 days
RSL Revival
5 days
FEL
5 days
RSL Revival
6 days
FEL
6 days
FEL
6 days
Liquipedia Results

Completed

BSL 2v2 Season 3
HSC XXVII
Heroes 10 EU

Ongoing

JPL Season 2
BSL Season 20
Acropolis #3
KCM Race Survival 2025 Season 2
CSL 17: 2025 SUMMER
Copa Latinoamericana 4
Championship of Russia 2025
RSL Revival: Season 1
Murky Cup #2
BLAST.tv Austin Major 2025
ESL Impact League Season 7
IEM Dallas 2025
PGL Astana 2025
Asian Champions League '25
BLAST Rivals Spring 2025
MESA Nomadic Masters
CCT Season 2 Global Finals
IEM Melbourne 2025

Upcoming

2025 ACS Season 2: Qualifier
CSLPRO Last Chance 2025
2025 ACS Season 2
CSLPRO Chat StarLAN 3
K-Championship
uThermal 2v2 Main Event
SEL Season 2 Championship
FEL Cracov 2025
Esports World Cup 2025
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
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.