해당코드를 이진코드로 만드는 것과 특정 문자열 이전까지만 읽게 하고싶습니다.

kbh999의 이미지

위에는 몇주전 제가 이리저리 찾아보며 만든 순차 탐색 단어찾기 소스입니다.

그런데 이번에는 이진탐색으로 변경시켜보려고 했는데 소스가 오류가 나더군요. 정말 난감합니다.

그리고 이번에는 읽어드리는 txt 파일에 여러 내용을 조합해서 특정 문자열 이전까지만 읽게하려고 했는

데 아직 공부가 부족해서 무슨 함수를 써야하고 어떻게 해야할지 모르겠습니다.

예를들어 bin:X:2:2::/usr/bin: 이런 word가 있다면 특정문자열 [:] 이전까지만 읽어들여서 결국bin 만

읽게 하고싶습니다.

int main(void)
{
	int number=1;
	int i,length;
	char input[20],word[2010];
	FILE *fp;//파일 포인터 선언
 
 
 
		if((fp=fopen("words.txt","rt+"))==NULL) //파일에서 문장들을 읽어오는 것으로 fp가 NULL값을 갖는 것은 포인터가 가르키는 값이 없다면 다음 문장을 출력하고 빠져나가는 문장
		{
			printf("파일을 찾을 수 없습니다.\n");
 
		}
		printf("영어단어 사전의 구성\n");
		printf("===================\n");
		printf("단어 읽는 중 ");
 
		 for(int j=0;j<2010; j++)
		{
 
			if(j % 100 == 0)
				printf(". ");
 
		}
				printf("총 2010단어의 사전구축\n");
				printf("영어 단어 찾기(Sequential Search) \n");
				printf("=============================\n");
 
 
		fseek(fp,0,SEEK_END); //fp의 파일포인터를 매겨변수로 해서 넘겨주면 fp의 포인터는 파일의 끝을 가르키게 된다.
 
		i=ftell(fp);//파일의 첫부분부터 파일 포인터가 위치한 곳까지의 거리를 리턴
 
		fseek(fp,0,SEEK_SET);//처음위치의 값
		length=ftell(fp);//현재 파일 포인터의 위치를 length에 저장
		length=i-length;
		while(1)
		{
		printf("단어 입력:");
			scanf("%s",input);
			while(number<length)
			{
				fscanf(fp,"%s",word);//txt파일을 읽으며 scanf
					if(strcmp(input,word)==0) //찾았을 경우 input 과 word를 비교해서 크면 양수 같으면 0, 작으면 음수를 출력
					{
						printf("단어 %s 찾음   비교회수:%d \n",input,number);
						for(int j=0;j<number; j++)
						{
							if(j % 100 == 0)
							printf(". ");
						}
						break;
					}
					else //찾지 못했을 경우
					{
						number++; //횟수증가
					}
 
			}
			if(length==number)
				printf("못찾았습니다\n");
			printf(" \n");
 
		}	
				return 0;
}
kbh999의 이미지

이런식으로 다른 프로그램을 써서 연구좀 해보려는데 너무 힘드네요

익명 사용자의 이미지

char* get_first_token( char *src , int delimiter )
{
  char *buff, *p;
 
  buff = strdup( src );
  p = strchr( buff , delimiter );
 
  if( p != NULL )
    *p = '\0';
 
 
  return buff;
}
 
int main()
{
  char *word = "bin:X:2:2::/usr/bin:";
  char *token = get_first_token( word , ':' );
 
  //token 에 "bin" 문자열이 담기게 됩니다.
 
  free( token );
 
 
  return 0;
}

--------

main() 에 모조리 넣으려고 하는건..좋지 않은 습관입니다.
함수로 쪼개세요!

kbh999의 이미지

지금도 열심히 c언어를 배우고있는데, 방법을 모르니 토익공부매진하느라 소홀히 한게 후회됩니다.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.