Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用C中的格式说明符从fprinft中的字符缓冲区读取索引的特定范围_C - Fatal编程技术网

使用C中的格式说明符从fprinft中的字符缓冲区读取索引的特定范围

使用C中的格式说明符从fprinft中的字符缓冲区读取索引的特定范围,c,C,我将试着详细描述我到目前为止遇到的问题和解决方案。我目前的解决方案不是最优的,我想让它变得更好 注意:char inputline[1024]没有特定的格式,它可以有任何格式的字符串。char inputline[1024]从一个包含数千行的文件中逐行读取。这就是为什么优化对我来说非常重要 FILE *outfile = fopen("tests.txt", "w"); int start_index = 1; //index from where I want to write the cha

我将试着详细描述我到目前为止遇到的问题和解决方案。我目前的解决方案不是最优的,我想让它变得更好

注意:
char inputline[1024]
没有特定的格式,它可以有任何格式的字符串。char inputline[1024]从一个包含数千行的文件中逐行读取。这就是为什么优化对我来说非常重要

FILE *outfile = fopen("tests.txt", "w");
int start_index = 1; //index from where I want to write the characters from the inputline array to the file
int prev_index = start_index; //keep track of last character position written to file

char inputline[1024] = {"12044 This is the test String 2019 to 2025 is another test& and 2050"};
int loop_count = 6; // This can vary as I have just hardcoded a value here for testing
int i;        
for(i=0; i<loop_count; ++i)
{
    fprintf(outfile, "%c", inputline[prev_index + i]);
}
prev_index = prev_index + loop_count;// this keeps track of last position of character written to file at end

问题是我正在阅读
%c”
,这不是很理想,我想用
%s
。但我无法理解如何转换
fprintf(outfile,“%c”,inputline[prev_index+I])
在写入文件时,通过在格式说明符中指定
prev_index+i
来使用
%s”
,因为我只希望根据要写入文件的循环,输入行[prev_index+i]中的字符。

您可以使用格式说明符
%.*s
并传入
长度

请注意,您必须在代码中包含

输出:

他的是


对不起,我没听懂。您能提供一个示例输入和预期输出吗?@Xatenev当然,让我更新问题我想我理解您的问题,使用
strncpy()
创建一个子字符串
inputline
start\u index
开始到您所支持的结束索引,并对
fprintf()进行一次调用
在从
strncpy()
@Xatenev()创建的新字符串上使用
%s
,我更新了问题。我不知道如何做到这一点,让你应该一些样本代码?如果我必须调用数组一百万次,那么使用strncpy是否有效?请查看,看看它是否有帮助。我使用了与您相同的变量名,这样您就知道了什么是什么(尽管像loop\u count这样的名称在这里不再有意义,因为不再有循环了)。请注意,您可能必须在代码中包含
Sample input in char inputline[1024] = {"This is my day in 2020 1230023"}

Desired output in tests.txt: (for loop will loop 6 times as loop_count is 6)
his is
#include <stdio.h>

int main(void) {
    int start_index = 1;
    int length = 6;
    char inputline[] = "This is my day in 2020 1230023";

    printf("%.*s\n", length, inputline + start_index); //  you can use fprintf(outfile, "%.*s", length, inputline + start_index); here
    return 0;
}
#include <stdio.h>
#include <string.h>

int main(void) {
    int start_index = 1;
    int loop_count = 6;
    char inputline[] = "This is my day in 2020 1230023";
    char substr[1024];

    strncpy ( substr, inputline + start_index, loop_count);
    substr[loop_count] = '\0';
    printf("%s\n", substr); //  you can use fprintf(outfile, "%s", substr); here
    return 0;
}