strtok

Using fgets() and strtok() to read in a file line-by-line in C?

不打扰是莪最后的温柔 提交于 2019-12-28 13:58:26
问题 I'm trying to use fgets and strtok() to read in a file line by line, and create a linked list of each different line of information. Right now, I'm only just putting the information into an array, just to try to figure out how to read the information in correctly, but it's not working right. In the while(fgets) portion, it seems to load everything into the array properly, and prints it out. However, after that loop has executed and I try to print out the whole array, I get really weird

在Linux下面开发一个mini版的shell

£可爱£侵袭症+ 提交于 2019-12-25 22:57:47
一.shell的原理 Linux系统提供给用户的最重要的系统程序是Shell命令语言解释程序。它不属于内核部分,而是在核心之外,以用户态方式运行。 其基本功能是解释并执行用户打入的各种命令,实现用户与Linux核心的接口。系统初启后,核心为每个终端用户建立一个进程去 执行Shell解释程序,shell的简单定义就是命令行解释器。 我现在打印一下当前进程的父进程。 编译运行后,打印出来的就是父进程,通过ps aux来查看该父进程的状态。我们可以很明显的看到当前test对应的进程------56042进程就是bash进程,而bash就是一个具体的shell。shell会f先ork一个子进程,它最后会被替换成我们敲入指令对应的代码和数据。 二.整体思路 打印一个提示符,让用户输入一个指令。 解析输入的指令,找到对应的可执行程序。 创建子进程,子进程程序替换(替换成你敲入的命令),来加载可执行程序。 父进程进行进程等待,等待子进程结束。 子进程结束,父进程从wait中返回,循环执行1. 1.打印一个提示符,让用户输入一个指令。 我们知道每次输入指令的时候,都会有一个提示符,我在这里为了简便暂时将这个提示符写死,不随着用户所在路径而变化。这个部分非常的简单,一个简单的printf就可以解决问题。 printf ( "[zhaotiedan@localhost myshell]$" ) ; 2

Previously stored strings are overwritten by fgets

泄露秘密 提交于 2019-12-25 17:16:03
问题 I'm reading records from a CSV file using fgets() to read the file one line at a time, and strtok() to parse the fields in each line. I'm encountering a problem where fgets() overwrites a string that was previously written, in favor of the new string. Here's an example of what I mean by that: record.csv (This is the file I'm reading in) John,18 Johann,29 main.c #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct customer { char *name; int age; } Customer; int main(void)

strtok() - Segmentation Fault [duplicate]

我的梦境 提交于 2019-12-25 06:38:44
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: strtok giving Segmentation Fault I try to use strtok function to split string in many tokens but in this example it returns me a seg. fault error. Where i'm in wrong?? #include <stdio.h> #include <string.h> int main(int argc, char** argv){ int i=0; char * string = "HI:HOW:ARE:YOU:?", *tmp; while(1){ if(i==0) tmp=strtok(string,":"); else tmp=strtok(NULL,":"); if(tmp==NULL) break; printf("%s\n",tmp); i++; } return

How to assign values dynamically to a struct

和自甴很熟 提交于 2019-12-25 03:28:20
问题 I am stumped as to how to access and change the values of a struct. The program takes in some external files and tokenized each string and categorizes them in the following fields of climate info. The external files look something like this: TDV format: TN 1424325600000 dn20t1kz0xrz 67.0 0.0 0.0 0.0 101872.0 262.5665 TN 1422770400000 dn2dcstxsf5b 23.0 0.0 100.0 0.0 100576.0 277.8087 TN 1422792000000 dn2sdp6pbb5b 96.0 0.0 100.0 0.0 100117.0 278.49207 TN 1422748800000 dn2fjteh8e80 6.0 0.0 100.0

strtok affects the input buffer

自作多情 提交于 2019-12-25 02:53:41
问题 I am using strtok to tokenise the string, Is strtok affects the original buffer? For e.g: *char buf[] = "This Is Start Of life"; char *pch = strtok(buf," "); while(pch) { printf("%s \n", pch); pch = strtok(NULL," "); }* printf("Orignal Buffer:: %s ",buf); Output is:: This Is Start Of life Original Buffer:: This I read that strtok returns pointer to the next token, then how the buf is getting affected? Is there way to retain original buffer (without extra copy overhead)? Follow-on Question::

insert strtok results into char* (increased dynamic)

五迷三道 提交于 2019-12-24 18:26:28
问题 I'm loosing my mind. I want to split string (char* text) with spaces and insert the string results into array and return this array. I have the following method in C char *read_command(char *text) { int index=0; char *res=NULL; char *command= (char*)malloc(strlen(text)+1); strcpy(command, text); char *tok = strtok(command, " "); while(tok!=NULL && index ==0) { res = (char*)realloc(res, sizeof(char)*(index+1)); char *dup = (char*)malloc(strlen(tok)+1); strcpy(dup, tok); res[index++] = dup; /

Assigning a char pointer to char array in C

妖精的绣舞 提交于 2019-12-24 13:42:12
问题 I am starting to studying C and I already run into few problems. I want to parse a file and store the results of each line in a structure. My structure looks like: struct record { char x[100]; } Then, whenever I use strtok to parse a line in some file.txt, struct record R; ... char *token; token = strtok(line, "\t"); token returns a pointer to the string and whenever I print it, it is correct string. I want to assign token to x, such as R.x = token , but I get an error, "char x[100] is not

C++ Splitting the input problem

那年仲夏 提交于 2019-12-24 10:20:07
问题 I am being given input in the form of: (8,7,15) (0,0,1) (0,3,2) (0,6,3) (1,0,4) (1,1,5) (2,1,6) (2,2,7) (2,5,8) (3,0,9) (3,3,10) (3,4,11) (3,5,12) (4,1,13) (4,4,14) (7,6,15) where I have to remember the amount of triples there are. I wrote a quick testing program to try read the input from cin and then split string up to get the numbers out of the input. The program doesn't seem to read all the lines, it stops after (1,1,5) and prints out a random 7 afterwards I created this quick testing

C : warning: assignment makes pointer from integer without a cast [enabled by default]

Deadly 提交于 2019-12-23 15:43:38
问题 This is my code #include<stdio.h> #include<stdlib.h> void main() { FILE *fp; char * word; char line[255]; fp=fopen("input.txt","r"); while(fgets(line,255,fp)){ word=strtok(line," "); while(word){ printf("%s",word); word=strtok(NULL," "); } } } This the warning I get. token.c:10:7: warning: assignment makes pointer from integer without a cast [enabled by default] word=strtok(line," "); ^ token.c:13:8: warning: assignment makes pointer from integer without a cast [enabled by default] word