簡體   English   中英

在 C 中使用 int 值更改字符串值

[英]Changing string value with int value in C

我有這個問題。

    printf("Welcome to The Daily Bugle\n");
    printf("------------Daily Press------------\n");
    printf("Title of the 1. news:");
    read_news_title("news\\1.txt");
    printf("Title of the 2. news:");
    read_news_title("news\\2.txt");
    printf("Title of the 3. news:");
    read_news_title("news\\3.txt");
    printf("Title of the 4. news:");
    read_news_title("news\\4.txt");

和 read_news_title 在這里

void read_news_title(char* file_path){
    FILE *fp=fopen(file_path,"r");
    char line_buffer[100];
    fscanf(fp,"%[^\n]",line_buffer);
    printf("%s\n",line_buffer);
}

我想像這樣轉換

for (int i=1; i<5; i++){
   printf("Title of the new %d",i);
   read_news_title("news\\[i].txt);
   }

我怎樣才能做到這一點?

是的,感謝評論部分,sprintf 正在為此工作。

我是這樣用的

for (int i=1; i<=4; i++){
printf("Title of the %d. news:",i);
sprintf(ch,"\"news\\%d.text\"",i);
printf("%s\n",ch );
}

奇跡般有效!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM