- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我是 C 的新手,我在拆分特定文本文件和将标记存储在数组中时遇到问题。
这是我的名为 data.txt 的 txt 文件,将被“|”分割:
Public|Jane|Q|Ms.|600|Maple Street|Your Town|Iowa|12345
Penner|Fred|R|Mr.|123|that Street|Winnipeg|MB|R3T 2N2
Gardner|Mark|E|Mr.|76|The Avenue|Toronto|ON|M5W 1E6
Acton|Hilda|P|Mrs.|66|What Blvd|Winnipeg|MB|R3T 2N2
我的代码是:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define INPUT_LENGTH 128
#define FIELD_LENGTH 30
#define NUM_FIELDS 9
int main( int argc, char *argv[] )
{
FILE *template = NULL;
FILE *data = NULL;
char input[INPUT_LENGTH];
char customerData[NUM_FIELDS][FIELD_LENGTH];
int element = 0;
char *next;
char ch;
template = fopen( "template.txt", "r" );
if ( template != NULL )
{
// read in the customers until we're done
data = fopen( "data.txt", "r" );
if ( data != NULL )
{
//next = strtok(data, "|");
while(fgets(input, INPUT_LENGTH,data) != EOF){
next = strtok(input, "|"); //splitting the data stored in input by |
while(next != NULL){
strcpy(customerData[element],next);
//customerData[element++] = next;
next =strtok(NULL, "|");
//element++;
}
//testing
for(element=0; element<INPUT_LENGTH; element++){
printf("%s\n", customerData[element]);
}
//printf("%s\n", input );
}
fclose( data );
}
fclose( template );
}
return EXIT_SUCCESS;
}
这样做会给我一个错误 array type 'char [30]' is not assignable
和一堆垃圾输出。我需要帮助才能知道我在拆分部分做错了什么。
代码中还有其他内容,因为我的程序的主要目的是拆分这些文件,将其存储在一个数组中并将每个位置用于我的模板:单独的 template.txt 文件:
Welcome back, $1!
We hope that you and all the members
of the $0 family are constantly
reminding your neighbours there
on $5 to shop with us.
As usual, we will ship your order to
$3 $1 $2. $0
$4 $5
$6, $7 $8
对于第一个标记,输出应该是这样的:
Welcome back, Jane!
We hope that you and all the members
of the Public family are constantly
reminding your neighbors there
on Maple Street to shop with us.
As usual, we will ship your order to
Ms. Jane Q. Public
600 Maple Street
Your Town, Iowa 12345
最佳答案
以下代码删除了对 template.txt
文件的引用,因为它未在代码中使用。
为客户数据定义了适当的结构
每个字段都从输入中提取并复制到客户数据数组中
“测试”代码输出客户数据数组中的每个字段。
实现错误检查并采取适当的措施。
#include <stdio.h>
#include <string.h> // strtok()
#include <stdlib.h> // exit(), EXIT_FAILURE
#define INPUT_LENGTH (128)
#define FIELD_LENGTH (30)
#define NUM_FIELDS (9)
#define MAX_CUSTOMERS (20)
struct customer
{
char lastName[ FIELD_LENGTH ];
char firstName[ FIELD_LENGTH ];
char initial[ FIELD_LENGTH ];
char salutation[ FIELD_LENGTH ];
char streetNumber[ FIELD_LENGTH ];
char streetName[ FIELD_LENGTH ];
char cityName[ FIELD_LENGTH ];
char stateName[FIELD_LENGTH ];
char zipCode[ FIELD_LENGTH ];
};
int main( void )
{
struct customer customerData[ MAX_CUSTOMERS ];
FILE *fp = NULL;
char input[INPUT_LENGTH];
int element = 0;
char *token = NULL;
// read in the customers until we're done
if( NULL == (fp = fopen( "data.txt", "r" ) ) )
{ // fopen failed
perror( "fopen to read data.txt failed");
exit( EXIT_FAILURE );
}
// implied else, fopen successful
while( element < MAX_CUSTOMERS && fgets(input, INPUT_LENGTH, fp))
{
if( NULL != (token = strtok(input, "|") ) ) //splitting the data stored in input by |
strcpy( customerData[element].lastName, token );
else
{ // invalid file format
fprintf( stderr, "line %d, contains invalid format\n", element);
fclose( fp );
exit( EXIT_FAILURE );
}
if( NULL != (token = strtok( NULL, "|") ) )
strcpy( customerData[element].firstName, token );
else
{ // invalid file format
fprintf( stderr, "line %d, contains invalid format\n", element);
fclose( fp );
exit( EXIT_FAILURE );
}
if( NULL != (token = strtok( NULL, "|" ) ) )
strcpy( customerData[element].initial, token );
else
{ // invalid file format
fprintf( stderr, "line %d, contains invalid format\n", element);
fclose( fp );
exit( EXIT_FAILURE );
}
if( NULL != (token = strtok( NULL, "|" ) ) )
strcpy( customerData[element].salutation, token );
else
{ // invalid file format
fprintf( stderr, "line %d, contains invalid format\n", element);
fclose( fp );
exit( EXIT_FAILURE );
}
if( NULL != (token = strtok( NULL, "|" ) ) )
strcpy( customerData[element].streetNumber, token );
else
{ // invalid file format
fprintf( stderr, "line %d, contains invalid format\n", element);
fclose( fp );
exit( EXIT_FAILURE );
}
if( NULL != (token = strtok( NULL, "|" ) ) )
strcpy( customerData[element].streetName, token );
else
{ // invalid file format
fprintf( stderr, "line %d, contains invalid format\n", element);
fclose( fp );
exit( EXIT_FAILURE );
}
if( NULL != (token = strtok( NULL, "|" ) ) )
strcpy( customerData[element].cityName, token );
else
{ // invalid file format
fprintf( stderr, "line %d, contains invalid format\n", element);
fclose( fp );
exit( EXIT_FAILURE );
}
if( NULL != (token = strtok( NULL, "|" ) ) )
strcpy( customerData[element].stateName, token );
else
{ // invalid file format
fprintf( stderr, "line %d, contains invalid format\n", element);
fclose( fp );
exit( EXIT_FAILURE );
}
if( NULL != (token = strtok( NULL, "|" ) ) )
strcpy( customerData[element].zipCode, token );
else
{ // invalid file format
fprintf( stderr, "line %d, contains invalid format\n", element);
fclose( fp );
exit( EXIT_FAILURE );
}
element++;
} // end while
//testing
#if 0
// code with bug
for(int i = 0; i < element; i++)
{
printf("lastName: %s\n", customerData[element].lastName);
printf("firstName: %s\n", customerData[element].firstName);
printf("initial: %s\n", customerData[element].initial);
printf("salutation: %s\n", customerData[element].salutation);
printf("streeNumber: %s\n", customerData[element].streetNumber);
printf("streetName: %s\n", customerData[element].streetName);
printf("cityName: %s\n", customerData[element].cityName);
printf("stateName: %s\n", customerData[element].stateName);
printf("zipCode: %s\n", customerData[element].zipCode);
}
#else
// corrected code
for(int i = 0; i < element; i++)
{
printf("lastName: %s\n", customerData[i].lastName);
printf("firstName: %s\n", customerData[i].firstName);
printf("initial: %s\n", customerData[i].initial);
printf("salutation: %s\n", customerData[i].salutation);
printf("streeNumber: %s\n", customerData[i].streetNumber);
printf("streetName: %s\n", customerData[i].streetName);
printf("cityName: %s\n", customerData[i].cityName);
printf("stateName: %s\n", customerData[i].stateName);
printf("zipCode: %s\n", customerData[i].zipCode);
}
#endif
fclose( fp );
return EXIT_SUCCESS;
} // end function: main
关于c - 如何拆分文本文件并将它们存储在数组中以用于 C 中的模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35044944/
我需要将文本放在 中在一个 Div 中,在另一个 Div 中,在另一个 Div 中。所以这是它的样子: #document Change PIN
奇怪的事情发生了。 我有一个基本的 html 代码。 html,头部, body 。(因为我收到了一些反对票,这里是完整的代码) 这是我的CSS: html { backgroun
我正在尝试将 Assets 中的一组图像加载到 UICollectionview 中存在的 ImageView 中,但每当我运行应用程序时它都会显示错误。而且也没有显示图像。 我在ViewDidLoa
我需要根据带参数的 perl 脚本的输出更改一些环境变量。在 tcsh 中,我可以使用别名命令来评估 perl 脚本的输出。 tcsh: alias setsdk 'eval `/localhome/
我使用 Windows 身份验证创建了一个新的 Blazor(服务器端)应用程序,并使用 IIS Express 运行它。它将显示一条消息“Hello Domain\User!”来自右上方的以下 Ra
这是我的方法 void login(Event event);我想知道 Kotlin 中应该如何 最佳答案 在 Kotlin 中通配符运算符是 * 。它指示编译器它是未知的,但一旦知道,就不会有其他类
看下面的代码 for story in book if story.title.length < 140 - var story
我正在尝试用 C 语言学习字符串处理。我写了一个程序,它存储了一些音乐轨道,并帮助用户检查他/她想到的歌曲是否存在于存储的轨道中。这是通过要求用户输入一串字符来完成的。然后程序使用 strstr()
我正在学习 sscanf 并遇到如下格式字符串: sscanf("%[^:]:%[^*=]%*[*=]%n",a,b,&c); 我理解 %[^:] 部分意味着扫描直到遇到 ':' 并将其分配给 a。:
def char_check(x,y): if (str(x) in y or x.find(y) > -1) or (str(y) in x or y.find(x) > -1):
我有一种情况,我想将文本文件中的现有行包含到一个新 block 中。 line 1 line 2 line in block line 3 line 4 应该变成 line 1 line 2 line
我有一个新项目,我正在尝试设置 Django 调试工具栏。首先,我尝试了快速设置,它只涉及将 'debug_toolbar' 添加到我的已安装应用程序列表中。有了这个,当我转到我的根 URL 时,调试
在 Matlab 中,如果我有一个函数 f,例如签名是 f(a,b,c),我可以创建一个只有一个变量 b 的函数,它将使用固定的 a=a1 和 c=c1 调用 f: g = @(b) f(a1, b,
我不明白为什么 ForEach 中的元素之间有多余的垂直间距在 VStack 里面在 ScrollView 里面使用 GeometryReader 时渲染自定义水平分隔线。 Scrol
我想知道,是否有关于何时使用 session 和 cookie 的指南或最佳实践? 什么应该和什么不应该存储在其中?谢谢! 最佳答案 这些文档很好地了解了 session cookie 的安全问题以及
我在 scipy/numpy 中有一个 Nx3 矩阵,我想用它制作一个 3 维条形图,其中 X 轴和 Y 轴由矩阵的第一列和第二列的值、高度确定每个条形的 是矩阵中的第三列,条形的数量由 N 确定。
假设我用两种不同的方式初始化信号量 sem_init(&randomsem,0,1) sem_init(&randomsem,0,0) 现在, sem_wait(&randomsem) 在这两种情况下
我怀疑该值如何存储在“WORD”中,因为 PStr 包含实际输出。? 既然Pstr中存储的是小写到大写的字母,那么在printf中如何将其给出为“WORD”。有人可以吗?解释一下? #include
我有一个 3x3 数组: var my_array = [[0,1,2], [3,4,5], [6,7,8]]; 并想获得它的第一个 2
我意识到您可以使用如下方式轻松检查焦点: var hasFocus = true; $(window).blur(function(){ hasFocus = false; }); $(win
我是一名优秀的程序员,十分优秀!