<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Geek Republic</title>
	<atom:link href="http://geekrepublic.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://geekrepublic.wordpress.com</link>
	<description>irc://irc.freenode.com/#geek_republic</description>
	<lastBuildDate>Tue, 11 Sep 2007 16:26:41 +0000</lastBuildDate>
	<language>pt-br</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='geekrepublic.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Geek Republic</title>
		<link>http://geekrepublic.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://geekrepublic.wordpress.com/osd.xml" title="Geek Republic" />
	<atom:link rel='hub' href='http://geekrepublic.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Ler linhas de um arquivo de texto de maneira portÃ¡vel</title>
		<link>http://geekrepublic.wordpress.com/2007/09/04/ler-linhas-de-um-arquivo-de-texto-de-maneira-portavel/</link>
		<comments>http://geekrepublic.wordpress.com/2007/09/04/ler-linhas-de-um-arquivo-de-texto-de-maneira-portavel/#comments</comments>
		<pubDate>Tue, 04 Sep 2007 18:15:17 +0000</pubDate>
		<dc:creator>Lealcy B. Junior</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[C ANSI]]></category>
		<category><![CDATA[Geek stuff]]></category>
		<category><![CDATA[ProgramaÃ§Ã£o]]></category>

		<guid isPermaLink="false">http://geekrepublic.wordpress.com/2007/09/04/ler-linhas-de-um-arquivo-de-texto-de-maneira-portavel/</guid>
		<description><![CDATA[Inspirado neste post, resolvi fazer minha prÃ³pria funÃ§Ã£o para ler linhas de um arquivo de maneira portÃ¡vel em C usando uma abordagem diferente. Segue o cÃ³digo: PS: Devido a alguns problemas no &#8220;syntax highlighting&#8221; da WordPress.com, prefira o cÃ³digo que colei na Pastebin, nos endereÃ§os abaixo: CÃ³digo: http://geekrepublic.pastebin.com/f2f654407 CabeÃ§alho: http://geekrepublic.pastebin.com/f1075175c AplicaÃ§Ã£o de teste: http://geekrepublic.pastebin.com/f26d908c Esse [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=geekrepublic.wordpress.com&amp;blog=1639124&amp;post=3&amp;subd=geekrepublic&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Inspirado <a href="http://codare.net/2007/02/28/c-ler-linhas-de-um-arquivo-texto-de-maneira-portavel/" target="_blank">neste post</a>, resolvi fazer minha prÃ³pria funÃ§Ã£o para ler linhas de um arquivo de maneira portÃ¡vel em <strong>C</strong> usando uma abordagem diferente. Segue o cÃ³digo:</p>
<p><em>PS: Devido a alguns problemas no &#8220;syntax highlighting&#8221; da WordPress.com, prefira o cÃ³digo que colei na Pastebin, nos endereÃ§os abaixo:</em></p>
<p><em>CÃ³digo: <a href="http://geekrepublic.pastebin.com/f2f654407" target="_blank">http://geekrepublic.pastebin.com/f2f654407</a></em></p>
<p><em>CabeÃ§alho: <a href="http://geekrepublic.pastebin.com/f1075175c" target="_blank">http://geekrepublic.pastebin.com/f1075175c</a><br />
</em></p>
<p><em>AplicaÃ§Ã£o de teste:  <a href="http://geekrepublic.pastebin.com/f26d908c" target="_blank">http://geekrepublic.pastebin.com/f26d908c</a></em></p>
<p><em>Esse PS serÃ¡ removido assim que a WordPress resolver o problema. </em></p>
<p><pre class="brush: cpp;">#include "fgetline.h"



char* fgetline(FILE* file)

{

	char* line = 0;

	fpos_t fpos;

	int size = 0, read = 0;

	

	if(feof(file)) return 0;



	/* Guardo a posi&Atilde;ƒ&Acirc;&sect;&Atilde;ƒ&Acirc;&pound;o atual no arquivo. */

	fgetpos(file, &amp;amp;fpos);

	

	/* Vejo o tamanho da linha. */

	read = fscanf(file, "%*[^\\n]%n", &amp;amp;size);



	/* Verifico se o fscanf() n&Atilde;ƒ&Acirc;&pound;o falhou ao ler o arquivo. */

	if(read &amp;lt; 0 || read == EOF) return 0;



	if(!size) /* Linha em branco. */

	{

		fsetpos(file, &amp;amp;fpos);

		fgetc(file); /* Engole a linha em branco. */

		

		/* Nada melhor para representar uma linha em branco que um calloc(1) :D. */

		return calloc(1, 1);

	}



	/* Tudo certo! Hora de alocar o retorno, reler a linha e retornar para o usu&Atilde;ƒ&Acirc;&iexcl;rio. */

	line = malloc(size + 1);

	if(!line) return 0;



	fsetpos(file, &amp;amp;fpos);



	/* J&Atilde;ƒ&Acirc;&iexcl; sei o quanto preciso ler, posso usar fread(). */

	read = fread(line, 1, size, file);



	if(read != size)

	{

		/* fread() falhou! */

		free(line);

		return 0;

	}



	/* Elimina o '\\n' do final da linha. */

	fgetc(file);



	/* Fecha a string. */

	line[size] = 0;



	return line;

}
</pre></p>
<p>O cabeÃ§alho:</p>
<p><pre class="brush: cpp;">/*

	Separando um arquivo em linhas em C, modo port&Atilde;ƒ&Acirc;&iexcl;vel e otimizado.



	por Lealcy Belegante Junior (lealcy &lt;-- ar_r0b=a --&gt; gmail PONTO com)

*/



#ifndef _FGETLINE_H_

#define _FGETLINE_H_



#include &lt;stdio.h&gt;

#include &lt;stdlib.h&gt;

#include &lt;string.h&gt;



/* Fun&Atilde;ƒ&Acirc;&sect;&Atilde;ƒ&Acirc;&micro;es: */

		

char* fgetline(FILE* file);



/*		L&Atilde;ƒ&Acirc;&ordf; o arquivo da posi&Atilde;ƒ&Acirc;&sect;&Atilde;ƒ&Acirc;&pound;o atual at&Atilde;ƒ&Acirc;&copy; encontrar um caractere de nova linha.

		Retorna um ponteiro para a string lida.

*/



#endif
</pre></p>
<p>Para testar:</p>
<p><pre class="brush: cpp;">#include &lt;stdio.h&gt;

#include &lt;stdlib.h&gt;

#include &lt;string.h&gt;

#include "fgetline.h"



int main(int argc, char **argv)

{

	FILE* file;

	char* line = 0;



	if (argc != 2)

	{

		printf("%s \\n", argv[0]);

		return 1;

	}



	file = fopen(argv[1], "r");

	if (!file)

	{

		perror(argv[1]);

		return 1;

	}



	while (line = fgetline(file))

	{

		printf("%i:%s\\n", strlen(line), line);

		

		free(line);

	}



	fclose(file);



	return 0;

}</pre></p>
<p>Ainda nÃ£o fiz nenhum <em>benchmark</em> para descobrir qual funÃ§Ã£o Ã© mais rÃ¡pida e nÃ£o verifiquei todas as falhas do meu cÃ³digo (aposto que tem algumas). Minha intenÃ§Ã£o era fazer a funÃ§Ã£o evitar vÃ¡rios <strong><em>realloc()</em></strong> seguidos e recursÃ£o.</p>
<p><u>Portanto, use com moderaÃ§Ã£o.</u></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/geekrepublic.wordpress.com/3/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/geekrepublic.wordpress.com/3/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/geekrepublic.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/geekrepublic.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/geekrepublic.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/geekrepublic.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/geekrepublic.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/geekrepublic.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/geekrepublic.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/geekrepublic.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/geekrepublic.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/geekrepublic.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/geekrepublic.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/geekrepublic.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/geekrepublic.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/geekrepublic.wordpress.com/3/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=geekrepublic.wordpress.com&amp;blog=1639124&amp;post=3&amp;subd=geekrepublic&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://geekrepublic.wordpress.com/2007/09/04/ler-linhas-de-um-arquivo-de-texto-de-maneira-portavel/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/18d3b0340ad6abcb825fa7415233a862?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Reign</media:title>
		</media:content>
	</item>
	</channel>
</rss>
