Skip to content

Instantly share code, notes, and snippets.

@yswallow
Last active March 23, 2018 06:17
Show Gist options
  • Select an option

  • Save yswallow/c7badb7353ba91ef8615ed54b9f5b07d to your computer and use it in GitHub Desktop.

Select an option

Save yswallow/c7badb7353ba91ef8615ed54b9f5b07d to your computer and use it in GitHub Desktop.
RubyとC言語でTab区切りのCSVっぽいのを配列に読み込む比較
fp = fopen(argv[1], "r");
if(! fp)
{
printf("can't open %s\n", argv[1]);
return 1;
}
i = 0;
mode = 0; /* 0=>name, 1=>point */
while ((ch = fgetc(fp)) != EOF) {
switch(ch)
{
case '\t':
if(mode==0)
users[i].name[j] = '\0';
mode = 1;
break;
case '\n':
i++;
users[i].point = 0;
mode = 0;
j = 0;
break;
default:
switch(mode)
{
case 0:
users[i].name[j] = ch;
j++;
break;
case 1:
users[i].point = users[i].point*10 + ( ch - '0' );
break;
}
}
}
fclose(fp);
str = File.read("./sys-prog-ex2-data.txt")
list = str.each_line.map do |line|
/\A([[:alpha:]]+)\t+(\d+)\n?\Z/ =~ line
[$1, $2.to_i]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment