라인단위 파일 내용 출력 프로그램

supaflow의 이미지

파일을 라인단위로 읽어서 원하는 라인부터 지정한 라인수만큼의 파일 내용을 출력하는 프로그램을 작성하고 있습니다.

fgets 으로 입력받아서 내용을 출력하긴 하겠는데

원하는 라인부터 지정한 라인까지 설정하는 부분이 어렵네요.

fseek을 사용하고자 하는데 바이트 단위로 파일 포인터가 옮겨져서 실질적으로 라인단위로 지정하기가 어렵습니다.

아이디어 좀 도와 주세요~

Jun92의 이미지

fgets이 리턴될때마다 카운트하시면 그게 라인 넘버인데...

=============================
야후!

supaflow의 이미지

라인 넘버가 필요한게 아니고 라인에 관한 정보가 필요한겁니다.

예를 들어서 20번째 줄부터 50번째 줄까지 내용을 출력하라 뭐 이런식으로요.

=====
http://supaflow.tistory.com

bootmeta의 이미지

파일 처음부터 일일이 이동하면서 개행 문자를 찾아 줄 정보를 기억 하는 방법 외에는 없을 것 같네요.

winner의 이미지

효율적인 처리법을 원하는 것 같은데 file 은 meta 정보가 없는 byte stream 일 뿐이랍니다.
Meta 정보는 programmer 의 몫이죠.

새로 file 을 작성하는 것이 아닌 기존의 file 들을 처리하기 위한 것일 뿐이라면 방법없습니다.

supaflow의 이미지

다음과 같이 프로그램을 작성했습니다.

#include <stdio.h>
 
void GetLine(char *buffer, int nLine, FILE *stream);
 
int main(int argc, char **argv)
{
       char lpszBuffer[256];
       FILE *input_fp,*output_fp;
 
       int start ;
       int end ;
 
       int i;
 
	   start = atoi(argv[1]); 			// atoi char -> int 형변환 ascii to int
	   end = atoi(argv[2]);	
 
 
	  // printf("%s %s %s %s %s \n",argv[0],argv[1],argv[2],argv[3],argv[4]);
 
 
	   if((input_fp = fopen(argv[3], "r")) == NULL ){
		   if(argc==3){
			   printf("fileoutput : Plase select Input file!\n");
			exit(1);   
		   }
	       printf("fileoutput : Can't find file!\n",argv[3]);	
		   exit(1);
	   }
 
	   if(argv[4]==NULL){
 
		   printf("\nfileoutput : create 'default' file!\n\n");
		   argv[4] = "default";
 
		}
	   if((output_fp = fopen(argv[4],"w")) < 0){
		   perror("fopen");
		   exit(1);
	   }
 
 
	   if(argc!=5 && argc!=4 ){
		   printf("Usage : ./fileoutput <startline> <endline> <inputfile> <outputfile>\n");
	   		exit(1);
	   }
 
		if( start > end ){
			printf("fileoutput : Wrong start state\n");
			exit(1);
		}
 
 
       for ( i = start; i <= end; i++ )
       {
            GetLine(lpszBuffer, i, input_fp); 
			//fprintf(stdout,"%d : ",i);      //print line count
			fprintf(stdout,"%s", lpszBuffer);
 
			//fprintf(output_fp,"%d : ",i);  // print line count
			fprintf(output_fp,"%s", lpszBuffer);
       }
 
 
       fclose(input_fp);
	   fclose(output_fp);
 
       return 0;
 
}
 
 
void GetLine(char *buffer, int nLine, FILE *input_fp)
 
{
 
       int i;
       rewind(input_fp);
       for ( i = 0; i < nLine; i++ )
       {
             fgets(buffer, 256, input_fp);
       }
 
}

여기서 원하는 라인이 파일의 줄수를 넘어설때 맨 마지막줄이 계속 반복되어서 나오거든요.
예를들면 c 소스의 마지막이 } 이니깐
}
}
}
}

이런 식으로 반복됩니다.
널값을 어떻게 넣는게 좋을까요..?

=====
http://supaflow.tistory.com

bushi의 이미지

...
   GetLine(lpszBuffer, start - 1, input_fp); 
 
   for ( i = start; i <= end && !feof(input_fp); i++ ) {
      GetLine(lpszBuffer, 1, input_fp); 
      fprintf(stdout,"%s", lpszBuffer);
      fprintf(output_fp,"%s", lpszBuffer);
   }
 
   fclose(input_fp);
   fclose(output_fp);
 
   return 0;
}
 
void GetLine(char *buffer, int nLine, FILE *input_fp)
{
   int i;
   for ( i = 0; i < nLine; i++ ) {
      fgets(buffer, 256, input_fp);
   }
}

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.