我正在努力将文件中的字符串打印到新文件中,并且似乎无法将其缠住,也无法使其正常工作,任何帮助都很好。
文件如下:
New York,4:20,3:03 Kansas City,12:03,3:00 North Bay,16:00,0:20 Kapuskasing,10:00,4:02 Thunder Bay,0:32,0:31
我正在尝试<Path Width="296" Height="296" Stretch="Uniform"
Fill="Red" Stroke="Lime" StrokeThickness="8"
Data="M100,50 L50,87.5 50,62.5 0,62.5 0,37.5 50,37.5 50,12.5Z"
RenderTransformOrigin="0.5,0.5">
<Path.RenderTransform>
<RotateTransform Angle="{Binding ElementName=slider, Path=Value}" />
</Path.RenderTransform>
</Path>
仅将文件名更改为名为 theCities.txt 的新文件。在我看来,逻辑是有意义的,但是就实现而言,我不知道如何fprintf
指向字符串的指针。任何帮助都会很棒。
fprintf
答案 0 :(得分:1)
您正在错误处理文件指针:
FILE *fpIn, *fpOut;
if (!(fpIn= fopen("yourfile.txt", "r"))) return -1;
if (!(fpOut=fopen("cities.txt", "w"))){fclose(fpIn); return -1;}
while (fgets(flightInfo[i], 1024, fpIn) > 0)
{
clearTrailingCarraigeReturn(flightInfo[i]);
// display the line we got from the fill
printf(" >>> read record [%s]\n", flightInfo[i]);
char *p = flightInfo[i];
for (;;)
{
p = strchr(p, ',');
fprintf(fpOut, "%s\n", p);
if (!p) break;
++p;
}
i++;
}
fclose(fpIn);
fclose(fpOut);
答案 1 :(得分:0)
您的代码有问题:
fp
,这非常令人困惑。"%.*s"
来输出字符串的一部分而不是行的结尾。这是修改后的版本:
FILE *outp = fopen("theCities.txt", "w");
for (i = 0; fgets(flightInfo[i], 1024, fp) > 0; i++) {
clearTrailingCarraigeReturn(flightInfo[i]);
// display the line we got from the fill
printf(" >>> read record [%s]\n", flightInfo[i]);
int city_length = strcspn(flightInfo[i], ",");
if (city_length) {
fprintf(outp, "%.*s\n", city_length, flightInfo[i]);
}
}
fclose(outp);
答案 2 :(得分:0)
只是更新我所做的事情,我使用了@Paul Ogilvie的方法并对其进行了调整,因此我仍然可以采用文件路径/名称的cmd行参数。然后,我改用strtok并跳过数字,因此它仅将城市名称输出到txt文件。谢谢大家的帮助!
FILE *fpIn, *fpOut;
if (!(fpIn = fopen(argv[1], "r"))) return -1;
if (!(fpOut = fopen("theCities.txt", "w+"))) { fclose(fpIn); return -1; }
while (fgets(flightInfo[i], 1024, fpIn) > 0)
{
clearTrailingCarraigeReturn(flightInfo[i]);
// display the line we got from the fill
printf(" >>> read record [%s]\n", flightInfo[i]);
char *p = flightInfo[i];
char *n = flightInfo[i];
char *c = flightInfo[i];
while(p != NULL)
{
p = strtok(p, ",");
n = strtok(p, "[1-9]");
c = strtok(p, ":");
if (!p) break;
while (n != NULL && c != NULL)
{
n = strtok(NULL, " ");
c = strtok(NULL, " ");
}
fprintf(fpOut, "%s\n", p);
p++;
p = strtok(NULL, " ");
}
i++;
}
fclose(fpIn);
fclose(fpOut);