파일 입력 및 포인터로 작업 한 것은 이번이 처음입니다. 코드가 엉망인 것 같아서 죄송합니다. 나는 다른 Stack Overflow 솔루션을 참고로보고 있었다. 내 코드는 입력 텍스트의 단어가 1000자를 초과하지 않는다고 가정 할 수있는 파일에서 가장 긴 단어를 찾습니다.C : 파일 입력 순서 정렬, 컴파일 오류가 많음
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// main() must return an integer
int main(int argc, char** argv) {
FILE *file; //open an existing file
file = fopen(argv[1], "r"); //using argv as a pointer to an array of pointers to arrays of characters
char *sentence = (char *) malloc(100 * sizeof(char)); //memory allocation, unsure what to use for size*
//fgets - reads a line from the specified stream and stores it into the string pointed to
//max amount set to 1000
while (fgets(sentence, 1000, file) != NULL) {
char *word;
int maxlen = 0;
char *maxW;
//max size of 1000 characters
maxW = (char *) calloc(1000, sizeof(char));
word = (char *) calloc(1000, sizeof(char));
word = strtok(sentence, " "); //using strtok to break sentance to token
//checking size of word with maxlen
while (word != NULL) {
if (strlen(word) > maxlen) {
maxlen = strlen(word);
strcpy(maxW, word);
}
word = strtok(NULL, " ");
}
printf("%s\n", maxW); //printing the max sized word
maxlen = 0; //reset
return 0;
}
}
난 단지 GCC를 사용하여 내 코드를 컴파일 창에 명령 줄을 사용하고 난 CLion를 사용하려하지만 순간에 CLion을 사용하는 방법을 알아낼 수 없습니다.
편집 : 이미지를 삭제했습니다.
문장의 malloc에 호출이 1,000 대신에 100을 곱한해야 하는가? – schil227
텍스트의 이미지는 가져 오지 않지만 텍스트는 게시하십시오. –
빌드 시스템 (또는 우리와 공유하지 않은 빌드 명령)이 깨져서 코드를 제쳐두고 (거기에 여러 가지가 결국 수정이 필요합니다). [귀하의 코드가 컴파일됩니다] (http://coliru.stacked-crooked.com/a/e2d337b637a7adf1) (이는 결코 "정확함"을 의미하지 않음). – WhozCraig