11 Replies - 48481 Views - Last Post: 19 December 2009 - 09:26 PM Rate Topic: -----

#1 x2x3i5x  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 18-December 09

How to use getline in c programming?

Posted 18 December 2009 - 11:50 PM

Hi,

I wrote a really simple program to count number of characters in a text file

#include "stdafx.h"
#include <stdio.h>

int main()
{
	int count;
	FILE *f;
	char s;

	f=fopen("output.txt","r");
	
	for(count = 0; s = fgetc(f) != EOF; count++);

	fclose(f);
	printf ("\n%d characters in output.txt.\n", count);

	return 0;	
}




I read somewhere that using fgets is dangerous and getline (or fgetline) is the recommended / preferred way. How do you use getline (or fgetline) to do exactly what my above program does? I kept seeing that getline/fgetline is a c++ function and I'm sort of lost on the syntax for getline/fgetline if indeed c programming can use it?

thanks in advance :)

Is This A Good Question/Topic? 0
  • +

Replies To: How to use getline in c programming?

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: How to use getline in c programming?

Posted 19 December 2009 - 12:16 AM

Have a read here
http://crasseux.com/...al/getline.html
Was This Post Helpful? 0
  • +
  • -

#3 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: How to use getline in c programming?

Posted 19 December 2009 - 02:16 AM

getline doesn't exist in ANSI standard C. - fgets perhaps isn't perfect, but its probably the safest option of all the standard functions available in <stdio.h>
Was This Post Helpful? 0
  • +
  • -

#4 olibenu  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 45
  • View blog
  • Posts: 537
  • Joined: 31-December 08

Re: How to use getline in c programming?

Posted 19 December 2009 - 03:04 AM

include iostream then use namespace std
Was This Post Helpful? 0
  • +
  • -

#5 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: How to use getline in c programming?

Posted 19 December 2009 - 03:09 AM

View Postolibenu, on 19 Dec, 2009 - 02:04 AM, said:

include iostream then use namespace std


That's no good for C now is it?
Was This Post Helpful? 0
  • +
  • -

#6 olibenu  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 45
  • View blog
  • Posts: 537
  • Joined: 31-December 08

Re: How to use getline in c programming?

Posted 19 December 2009 - 03:19 AM

y not? getline still works with char arrays. besides he was specific: 'how to use getline in c' is there another way?
Was This Post Helpful? 0
  • +
  • -

#7 Guest_c.user*


Reputation:

Re: How to use getline in c programming?

Posted 19 December 2009 - 03:42 AM

x2x3i5x said:

I read somewhere that using fgets is dangerous

it is dangerous to use such small variable for the counter
use size_t count;
if you want to use getline (based on fgets certainly) it is not a very good idea because you will do more operations than you have now (fgets needs an array, then you will need strlen, I think, an so on)

the dangerous function is gets (it hasn't the size limiter)

This post has been edited by c.user: 19 December 2009 - 03:45 AM

Was This Post Helpful? 0

#8 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: How to use getline in c programming?

Posted 19 December 2009 - 04:19 AM

View Postolibenu, on 19 Dec, 2009 - 02:19 AM, said:

y not? getline still works with char arrays. besides he was specific: 'how to use getline in c' is there another way?


1 - Write a little sample program.
Ensure you include iostream
Try and compile it in a C compiler.
(Not a C++ compiler)
Do you get something like this?
"error: iostream: No such file or directory"
You should.

2 - What role does "namespace std;" have in C?
Was This Post Helpful? 0
  • +
  • -

#9 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: How to use getline in c programming?

Posted 19 December 2009 - 04:45 AM

its completely nonsensical - there is no way to use "namespace std" in C because it doesn't exist

Its the equivalent to as asking "How do I use the 'final' keyword in C++? There must be a way because it works in Java!"
Was This Post Helpful? 0
  • +
  • -

#10 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: How to use getline in c programming?

Posted 19 December 2009 - 04:55 AM

Nice one Bench!!!

You phrase things so much better than I could.
Was This Post Helpful? 0
  • +
  • -

#11 x2x3i5x  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 18-December 09

Re: How to use getline in c programming?

Posted 19 December 2009 - 09:22 PM

View Postjanotte, on 19 Dec, 2009 - 03:55 AM, said:

Nice one Bench!!!

You phrase things so much better than I could.


So hmm, you can't use getline in C? Ok. I did a bit more research and found that getline can be used if you're using the #include<stdio.h> from the GNU but getline is not a part of C language.

You'll need C++ language to do it I guess then.

So everyone agree that fget(s) ilike my code has, s the only way to go in C language in terms of wanting to count number of characters in a given text file?
Was This Post Helpful? 0
  • +
  • -

#12 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5191
  • View blog
  • Posts: 26,899
  • Joined: 10-May 07

Re: How to use getline in c programming?

Posted 19 December 2009 - 09:26 PM

Just get each char at a time & evaluate it. If it's not enter, then it's not the end of the line, & if it the current running integer (array element) isn't larger than the size of your buffer array, append it to your input char array.

Simple :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1