- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我是编程新手,我不确定如何解决这个问题。我试着做 %8s ... %52s 但它没有考虑到分隔符应该占用子字符串中的空间。因此,如果我尝试扫描 8 个空格,一个长度为 5 的单词、一个空格和另一个长度为 8 的单词,它将获取第一个字母字符串而不是空格。这是一次从文件中读取一行,但将其分解为子字符串,其中每个子字符串都放入一个唯一的数组中。例如:
Word AnotherW
第一个数组将包含 Word
,而不是
(8 个空格)。
char firCol[8]; //first 8 characters including delimiters
char space[1] = {' '}; //guaranteed space
char secCol[1] = {' '}; //space or '+'
char thirCol[7]; //word of length 7 but if shorter includes delitimers
char forCol[1] = {' '}; //guaranteed space
char fifCol[10]; //sames of 7 but length
char comm[52]; // length 52 char and delimitrer
memset(firCol, ' ', 8);
memset(thirCol, ' ', 7);
memset(comm, ' ', 52);
f = fopen(argv[1], "r");
memset(info, 0, sizeof(struct Account) * TABLE_SIZE);
//Initialize the array, set all indices equal 0
while(EOF != fscanf(f, "%8c %1c %1c %7c %1c %10c %52c", firCol, space, secCol, thirCol, forCol, fifCol, comm))
{
pass1(info, firCol, space, secCol, thirCol, forCol, fifCol, comm);
}
/******************************/
/***MORE INFO ADDED******/
/******************************/
Col 1-8 label optional
Col 9 blank
Col 10 + optional
Col 11-17 mneumonic
Col 18 blank
Col 19 #, @, = ... optional
Col 20-29 (operand) label, register, ',',X optional ...
Col 30-31 blank
Col 32-80 comments optional (NO PERIOD REQUIRED)
ADDRES START 100
. tests pc forward addressing
. tests base addressing
. tests pc backward addressing
NOW +LDB #BEGIN load base register
XXX BASE BEGIN tell assembler
YYY LDA BE A <- (m..m+2)
EN RESW 4
BE WORD 2
BEGIN RESW 1
JUNK RESW 2200
THERE RESW 1
ZZZ LDA JUNK
MMM STA THERE
END NOW
RESULTS:
100 ADDRES START 100
. TESTS PC FORWARD ADDRESSING
. TESTS BASE ADDRESSING
. TESTS PC BACKWARD ADDRESSING
100 NOW +LDB #BEGIN LOAD BASE REGISTER
104 XXX BASE BEGIN TELL ASSEMBLER
104 YYY LDA BE A <- (M..M+2)
107 EN RESW 4
113 BE WORD 2
116 BEGIN RESW 1
119 JUNK RESW 2200
1ae1 THERE RESW 1
1ae4 ZZZ LDA JUNK
1ae7 MMM STA THERE
1aea END NOW
Table Location Label Address Use Csect
135 0 BE 113 main main
147 0 EN 107 main main
231 0 MMM 1ae7 main main
244 0 NOW 100 main main
264 0 XXX 104 main main
267 0 YYY 104 main main
270 0 ZZZ 1ae4 main main
312 0 JUNK 119 main main
357 0 BEGIN 116 main main
376 0 THERE 1ae1 main main
435 0 ADDRES 100 main main
最佳答案
首先,正确的方法:
#include <stdio.h>
int main(int argc, char **argv)
{
//TODO: Name this variables better. I cannot stress how important naming this is
char firCol[9]; //first 8 characters including delimiters
char plusFlag; //space or '+'
char thirCol[8]; //word of length 7 but if shorter includes delitimers
char fifCol[10]; //sames of 7 but length
char comm[53]; // length 52 char and delimitrer
FILE* f; //Don't forget variables. And when asking questions with code, it's best to include everything
//Don't worry about initializing the strings. In general, initializing variables is a good idea, but these will be clobbered.
f = fopen("file.txt", "r");
//Ah, dead code. It might have been something at some point. But it was killed. Now it's corpse litters the field of battle.
//memset(info, 0, sizeof(struct Account) * TABLE_SIZE); //Initialize the array, set all indices equal 0
//the spaces in the file are eaten by the spaces in the string literal
//The size of the strings can vary, but the number in %8s is the maximum. It's a good safety precausion.
// %*c eats a character and throws it away. We do this because there might be one or two whitespaces. The second of which may be important. If we put a whitespace in the string literal, it would have eaten both, throwing off the rest of the command.
//The %c is always just one character. You don't want to read in a string for that + symbol, as it isn't terminated by a null character. %1s would work... but no.
//The %c is tied to &plusFlag. The ampersand indicates the address of the variable. The other variables are arrays, and act like the address of the first character in the array.
while(fscanf(f, "%8s%*c%c%7s %9s %52s", firCol, &plusFlag, thirCol, fifCol, comm) != EOF)
{
printf("%s %c %s %s %s\n", firCol, plusFlag, thirCol, fifCol, comm);
}
return 0;
}
对于不同程度的“妥当”。我的意思是,您真的确定数据格式正确吗?该文件中甚至还有数据?有文件吗?仔细检查所有可能发生的事情是至关重要的 DO-178c 代码和高中演示之间的区别。那和一大堆文档。
从技术上讲,您所做的应该有效。你的 fscanf 中只有额外的空间。所以它扫描了 8 个字符,将它们放入 firCol,然后吃掉一个你认为会进入 space
的空格,然后将 +
放入 space。尝试:
fscanf(f, "%8c%1c%1c%7c%1c%10c%52c", firCol, space, secCol, thirCol, forCol, fifCol, comm))
这确实从文件中读取数据并将其放入您的变量中……但我不会称其为“正确的”。如果你想使用字符串,你会发现如果你用 null 终止它们,它与编程世界的其他部分更兼容。这符合……呃……常识。虽然剥猫皮的方法有上百万种,但与所有其他程序员、库和 API 一起效仿会让你的生活更轻松。
C 中的字符数组,也称为字符串,需要在它们的末尾有一个空终止字符 '\0'
。 '\0'
与二进制值 0
相同,但与 ascii 字符 '0'
不同。
注意引号。在 C 中,单引号中的任何内容都是 char
。 'a'、'b'、'9'、'0',以及诸如 '\n'、'\0' 或 '\b' 之类的命令字符。引号中的任何内容都是字符串文字。 “这是一个字符串文字”
。 char
字符几乎只是在 ascii 表上执行查找。另一方面,字符串表现得有点古怪,有很多陷阱让人们学习这门语言。使 C 字符串更易于处理是催生其他语言的主要力量之一。
看起来你想把这一切都推到一个结构中。你可以这样做:
//Outside of main
typedef struct
{
char firCol[9]; //first 8 characters including delimiters
char plusFlag; //space or '+'
char thirCol[8]; //word of length 7 but if shorter includes delitimers
char fifCol[10]; //sames of 7 but length
char comm[53]; // length 52 char and delimitrer
} DataFromFile_t;
int main(int argc, char **argv)
{
DataFromFile_t dataFromFile;
. . .
fscan(f,"%s ...", dataFromFile.firstCol);
让我看看还有什么......
关于c - 如何正确地将一定数量的多个字符的文件读入 C 中的 char 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29130413/
假设您有 2 个文件,如下所示。 file_1_october.csv file_2_november.csv 文件具有相同的列。所以我想在 R 中读取这两个文件,我可以使用 map 轻松完成。我还想
我有一个制表符分隔的文本文件: 0730000 John 1 01 225 000 000 当我将它读入 R 时 stud_stats data.table::f
似乎最直观的是 .rdata 文件可能是 R 加载的快速文件格式,但是在扫描一些堆栈帖子时,似乎更多的注意力集中在提高 .csv 或其他格式的加载时间上。有确定的答案吗? 最佳答案 不是一个明确的答案
我是 R 的新手,目前在读取 .csv 文件并将其转换为 data.frame 时遇到了很多麻烦7 列。这是我正在做的: gene_symbols_table head(gene_symbols_t
基本上我有一个格式如下所示的 csv: csv 有 11 列,前五列和后五列完全相同。我希望能够读取 csv 并将第一列和第五列(期间和支出)的所有实例存储在一个列表中,它们具有值,并对另一个列表中的
我对 Julia 比较陌生,正在寻找一种有效的方法来从文本文件中读取并将每个“列”存储在数组中(我有 2 列,但通用解决方案也很棒)。例如,我想要输入 1 2 3 4 5 6
基本上我有一个格式如下所示的 csv: csv 有 11 列,前五列和后五列完全相同。我希望能够读取 csv 并将第一列和第五列(期间和支出)的所有实例存储在一个列表中,它们具有值,并对另一个列表中的
我的程序分配了一个 32 位 int,随后尝试使用 read(2) 从套接字将 4 个字节读入 int 有时读取不完整并返回读取 2 个字节。有什么方法可以从中恢复吗?我想我必须在 int 的中途生成
我有大量的 CSV 文件。有些标题从第一行开始,其他标题从第 3 行开始,其他的从第 7 行开始,依此类推。 标题看起来都一样,它们只是从不同文件的不同行开始。有没有办法有条件地 read.csv 文
我写了一个小程序来从 csv 文件中读取数据: using System; using System.Collections.Generic; using System.Linq; using Sys
我需要读入一个包含 10,000 个整数的列表,并将它们按升序放置在一个 vector 中。请注意,我不是在然后阅读排序,而是在同时阅读时排序。 我这样做是为了学习。我意识到阅读时排序是 O(n^2)
我有一个问题。不幸的是,我没有找到任何答案。如何将参数传递给脚本,这是另一个命令的结果。例如: ls | ./myscript.sh 我想将 ls 的结果传递给 myscript。如果我执行上面的命
我在读取扩展 ASCII 字符并将其转换为十进制值时遇到问题。我试过这样做: unsigned char temp; while(temp = cin.get != EOF) { cout << (i
我已经通过以下命令加载了文本文件。我想从 contents 中删除由 \n 分隔的第一行标题行。怎么做? txtfile = open(filepath, "rt") contents = txtfi
希望一切顺利...我正在将数据集输入到 sklearn 算法中进行分类,但找不到任何简单的数据集来开始,所以我自己制作了数据集。但有一个问题... import numpy as np import
我有一个 .csv 文件,它有 3 行和 5 列,值为 0、1、2、3、50 或 100。我将它从 Excel 工作表保存到 .csv 文件。我正在尝试使用 C++ 读取 .csv 文件,并根据最后三
我有一个 HTML 文件,它将作为我要发送的电子邮件的模板。 html 中有一些字段是可变的。我想知道是否有一种可靠的方法可以用变量替换 HTML 文件中的占位符。我知道我可以 string.Repl
我从未使用过 JSON 文件,但我有实现 JSON 文件的任务,我需要将其转换为 IEnumerable。当我尝试对 JSON 对象进行反序列化时,我得到一个异常,上面写着: An unhandled
我正在尝试阅读 IFormFile从这样的 HTTP POST 请求中收到: public async Task UploadDocument([FromForm]DataWrapper data)
我有一个包含大量多行文本 block 的文件。我想将该文件读入一个字符向量列表——每个 block 一个。我对 scan()、read.table() 等函数的文档的阅读似乎表明一行的结尾将结束向量。
我是一名优秀的程序员,十分优秀!