홈>
입력 파일을 가져 와서 색인을 만들고 다른 파일을 저장하려고합니다.
입력 파일을 그런 식으로 내 프로그램으로 리디렉션하면 모든 것이 제대로 작동합니다
./myprog <text.txt
그러나 argv [1]를 사용하여 명령 행에서 인수로 파일을 열려고하면 작동하지 않으며 이유를 이해할 수 없습니다
내 파일을 여는 방법으로 무언가를 추측한다
전체 코드를 게시하지만 파일을 읽을 때 문제가 코드 상단에 있다고 생각합니다
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXW 1000
#define MAXC 100
typedef struct {
char seen[MAXC];
char lines[1024];
} wfstruct;
int get_word_freq (wfstruct *words, size_t *idx, FILE *fp);
int compare (const void *a, const void *b);
int main (int argc, char **argv) {
printf("%d",argc);
printf("%s",argv[1]);
/* initialize variables & open file or stdin for seening */
wfstruct words[MAXW] = {{{ 0 }, 0}};
size_t i, idx = 0;
FILE *fp2;
if(argc>2)
{
printf("too much args\n");
return 1;
}
if(argc<2)
{
printf("give me a file\n");
return 1;
}
FILE *fp=fopen(argv[1],"r");
get_word_freq (words, &idx, fp);
/* sort words alphabetically */
qsort (words, idx, sizeof *words, compare);
fp2 = fopen("Output.txt", "w");
fprintf(fp2, "The occurences of words are");
printf ("\nthe occurrence of words are:\n\n");
for (i = 0; i < idx; i++)
{
fprintf(fp2, " %-28s : seen in lines %s \n", words[i].seen, words[i].lines);
printf (" %-28s : seen in lines %s \n", words[i].seen, words[i].lines);
}
fclose(fp2);
return 0;
}
int get_word_freq (wfstruct *words, size_t *idx, FILE *fp)
{
size_t i;
/* read each word in file */
char *word;
word = malloc(sizeof(char));
int now;
int line = 1;
int j=0;
for (;;j++)
{
now=getchar();
if(now==EOF)
{
break;
}
if(!isalpha(now)){
word[j] = '\0';
j=-1;
for (i = 0; i < *idx; i++) {
/* if word already 'seen', update 'words[i]. freq' count */
if (strcmp (words[i].seen, word) == 0) {
sprintf(words[i].lines + strlen(words[i].lines),"%d,",line);
goto skipdup; /* skip adding word to 'words[i].seen' */
}
} /* add to 'words[*idx].seen', update words[*idx].freq & '*idx' */
strcpy (words[*idx].seen, word);
sprintf(words[*idx].lines,"%d,",line);
(*idx)++;
skipdup:
if (*idx == MAXW) { /* check 'idx' against MAXW */
fprintf (stderr, "warning: MAXW words exceeded.\n");
break;
}
if(now=='\n'){
line++;
}
continue;
}
now=tolower(now);
word[j]=now;
word=realloc(word,(j+1+1)*sizeof(char));
}
fclose (fp);
return 0;
}
/* qsort compare funciton */
int compare (const void *a, const void *b)
{
wfstruct *ap = (wfstruct *)a;
wfstruct *bp = (wfstruct *)b;
return (strcmp (ap->seen, bp->seen));
}
- 답변 # 1
관련 자료
- c++ - 구분 기호가있는 텍스트 파일에서 읽기
- .net core - C #에서 매개 변수로 텍스트가있는 Csv 파일 읽기
- c++ - 파일에서 모든 데이터 읽기
- java - Google 드라이브에서 txt 파일을 읽는 방법은 무엇입니까?
- python - 이로부터 py 파일을 어떻게 얻습니까?
- c# - NetworkStream에서 데이터 읽기
- r - 알 수없는 인코딩 (FDF)으로 파일을 읽는 방법
- python - PyQt5로 파일을 저장하는 방법은 무엇입니까?
- python - Falcon respstream에서 xlsx 파일을 얻는 방법
- javascript - src 파일에서 indexhtml로 js 파일을 가져 오는 방법은 무엇입니까?
- python - xls 파일에서 defined_names를 가져옵니다
- c# - 3 대의 서버에서 파일 스트림
- python - 파이썬 - sql 쿼리의 json 파일
- javascript - 삭제 이벤트에서 CSV 파일에서 데이터 읽기
- c# - 에서 입력 한 DropDownList
- intellij idea - 작업 디렉토리에서 코드로 파일 추가
- c++ - 파일 데이터의 동적 클래스 유형
- javascript - 파일로 양식을 가져 와서 넷 코어에서받는 방법은 무엇입니까?
- wpf - C #으로 ini 파일을 읽을 수 없습니다
- image - flutter - changenotifier에서 파일을 가져올 수 없습니다
트렌드
- OpenCv의 폴더에서 여러 이미지 읽기 (python)
- 파이썬 셀레늄 모든 "href"속성 가져 오기
- html - 자바 스크립트 - 클릭 후 변경 버튼 텍스트 변경
- javascript - 현재 URL에서 특정 div 만 새로 고침/새로 고침
- JSP에 대한 클래스를 컴파일 할 수 없습니다
- JavaScript 변수를 HTML div에 '출력'하는 방법
- git commit - 자식 - 로컬 커밋 된 파일에 대한 변경을 취소하는 방법
- jquery - JavaScript로 현재 세션 값을 얻으시겠습니까?
- javascript - swiperjs에서 정지, 재생 버튼 추가
- python - 화면에서 찾은 요소를 찾을 수없는 경우 셀레늄
먼저 코드를 포맷하고 두 번째로 fopen ()으로 파일을 연 후 getchar ()로 파일을 읽으려고합니다. getchar ()이 stdin에서 읽으므로 작동하지 않습니다. fopen ()으로 연 파일에서 문자를 읽으려면 fgetc ()를 사용해야합니다.
http://www.cplusplus.com/reference/cstdio/fgetc/
함수 get_word_freq () 변경
now=getchar();
에 와이즈 비즈now=fgetc(fp);