내가 파일의 라인으로 라인을 읽고 내 목록에 각 행의 첫 번째 대상을 저장 내 프로그램에서 내용c에서 동일한 메모리 위치로 값을 저장하는 방법?
123 1234
1223 124235
21432 325
의 파일 스트림이있는 경우. 이 줄은 같은 위치에 있으며 프로그램을 실행하면 가장 최근의 데이터를 계속 가리키고 목록에 배치합니다. 즉, while 루프에서 printL()이라는 함수가 있으면 의미합니다. 그것은 아닌 경우 확인 후 insertIntoList()
를 호출하기 전에 strtok()
에서 반환 단지 strdup()
를 추가
123/
1223/1223/
21432/21432/21432/
대신
로드 된 각 필드의 내용을 유지하기 위해123/
123/1223/
123/1223/21432
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct n{
char *value;
struct n *next;
} Node;
void printList(Node *head){
Node *cur = head;
while(cur!=NULL){
printf("%s/", cur->value);
cur = cur->next;
}
printf("\n");
}
void insertIntoList(Node **head, char *data){
Node *newNode = malloc(sizeof(Node));
if (newNode == NULL){
perror("Failed to allocate a new node for the linked list");
exit(1);
}
newNode->value = data;
newNode->next = NULL;
Node *currentList = *head;
if(*head == NULL){ //if the linked list head is null, then add the target into linked list
*head = newNode;
}
else{
while(currentList->next!=NULL){
currentList = currentList->next;
}
currentList->next = newNode;
}
}
int main(int argc, char**argv){
FILE *fileStream;
size_t len = 0;
char *line = NULL;
Node *head = NULL;
int j;
for(j=1; j<argc-2;j++){
fileStream = fopen(argv[j], "r");
if(fileStream == NULL){
fprintf(stderr, "could not open");
continue;
}
insertIntoList(&head,"a"); /////////////Line 95
insertIntoList(&head,"b");
insertIntoList(&head,"c");
insertIntoList(&head,"d");
printf("here is a try\n");
printList(head);
while(getline(&line, &len, fileStream)!=EOF){ /////////////Line 101
char *targetNum = strtok(line, " \t\r\n");
printf("*****%s\n", targetNum);
insertIntoList(&head, targetNum);
printf("######print head here is##########\n");
printList(head);
printf("######print head here is##########->\n");
}
//printList(head);
}
return 0;
}
그리고 질문은 ...? – MordechayS
@MordechayS 어떻게 값을 저장해야합니까 ... "123/1223/21432"대신 "21432/21432/21432 /"와 같은 현재 값을 인쇄합니다 ... 지적한 값을 저장하는 방법을 모르겠습니다. ... – HxH
'insertIntoList'코드를 추가하십시오. – MordechayS