- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
提供代码 M.R.E。
症状是“简单”:
hello\nhello\nhelloEOF
并插入 '_
' @ ln=2
& col=1
(或者更确切地说是任何 line>1 && col==1
)程序奇怪地做对了,但是吃掉了 '\n'
,导致第 2 行与第 1 行 融合 - hello_hello\nhelloEOF
,而不是 hello\n_hello\nhelloEOF
。hello\nhello\nhelloEOF
的最后一行中插入 '_
' @ ln=3
& col=5
(或者更确切地说,在由 EOF 终止的行中的任何字符 col>1
)导致它跳过最后一个实际字符行:hello\nhello\nhell_EOF
而不是 hello\nhello\nhell_oEOF
。col==1
in line terminated by EOF)导致错误消息(\n Invalid Index.
)扔了。就我的直觉而言,我认为在处理行终止 ('\n'
,EOF
) 及其计算方式时存在缺陷通常作为循环计数器,但我不明白。
想法/算法:
EOF
。下面的代码完成了所有真正的对话:
#include <stdio.h>
#include <string.h>
/* Likely error region in chknmove(), chkncpy() and/or main() ;
All other functions used are also provided */
long long lncnt(FILE *lcount){
/* counts the number of lines in a file with fgets() */
rewind(lcount); /* ensures lines counted from beginning */
long long lines=0; char line[501];
while((fgets(line,501,lcount))!=NULL){
lines++;
}
rewind(lcount); /* leaves fptr @ 0,0 rather than EOF */
return lines;
}
int chknmove(FILE *tomove, long long line, long long col){
/* Function to check & move FILE* to certain line/col */
rewind(tomove); /* make sure fptr is at beginning of file */
int f=0; /* set check-variable to 0 for successful */
if(line<1 || col<1)
f = -1; /* Ln,col cannot be -ve or even 0 -> 1st col is @ 1ln,1col in any non-empty file */
else{
long long i,q; i=1;q=0; /* i = lines seen, q = cols seen */
/* i init to 1 for ease of comprehension v/s testing 'line-1' */
while(i<line || q<col){ /* loop must iterate until i==line && q==col */
int ch = fgetc(tomove); /* int to store EOF */
if(ch==EOF){
f=-1; break; /* if file ends before reaching index, failed. */
}
else if(ch=='\n') {
if(i==line){
f=-1; break;/* if line ends before reaching col in ln, failed. */
}
i++; q=0; /* else , increase i by 1 and set q (col) to 0 */
}
else
q++; /* any other character is 'normal' , just increment q (col) by 1 */
}
}
if(f==0){
fseek(tomove,-1,SEEK_CUR);
/* since index must be checked, loop reaches it -> fseek(-1) causes *next* r/w to fptr to be on the index . */
return 0;
}
else{
/* f != 0 : moving has failed. */
printf("\n Invalid Index.\n");
return -1;
}
}
int chkncpy(FILE *source, FILE *dest,long long beginln, long long begincol, long long endln, long long endcol){
rewind(source); int f=0;
if(beginln<1 || begincol<1 || endln<beginln || endcol<1 || (beginln==endln && endcol<begincol))
f=-1; /* line/col must be >= 1 in non-empty file, copying done top to bottom (begin-index <= end-index ) */
else{
if(chknmove(source,beginln,begincol)==0){
/* loop begins if begin-index is valid */
long long i,q; i=beginln;q=begincol; /* i = lines, q = cols */
while(i<endln || q<endcol){
/* while end-index is not reached : !(i==endln && q==endcol) */
int ch = fgetc(source);
if(ch==EOF) {f=-1; break;} /* File ends b/w begin-index & end-index - failed. */
else if(ch=='\n') {
if(i==endln) {f=-1; break;} /* endln ends before reaching endcol - failed */
i++; q=0;
fputc(ch,dest); /* valid '\n' put in dest file */
}
else{
q++; fputc(ch,dest); /* valid char put in dest file */
}
}
}
else f=-1; /* if begin-index is invalid, failed. */
}
if(f==-1){
/* if f != 0 : chkncpy() failed */
printf("\n Invalid Index.\n");
return -1;
}
else /* if f remains == 0 */ return 0;
}
long long lastlncol(FILE *fyl){
rewind(fyl); /* makes sure fptr @ beginning */
chknmove(fyl,lncnt(fyl),1); /* move to last line */
long long cnt=0; /* cnt stores no. of chars in last line */
while(1){
int g = fgetc(fyl);
if (g==EOF)
break; /* EOF not counted */
else if(g=='\n'){
cnt++;break; /* \n is counted as a char, but also as EOL */
}
else
cnt++; /* all other chars counted */
}
rewind(fyl); /* leaves file at its beginning */
return cnt;
}
long long lcc(FILE *fyl,long long line){
rewind(fyl); int f = chknmove(fyl,line,1); /* moves fptr to line */
long long cnt=0;
if(f==0){
while(1){
int g = fgetc(fyl);
if (g==EOF)
break;
else if(g=='\n'){
cnt++;break;
}
else
cnt++;
}
rewind(fyl);
return cnt;
}
else
return -1;
}
void writer(FILE *write, int ctrl){
printf("\n Terminate Input with \"/end/\".\n\n Type below :\n\n");
char in[501]; /* str that stores input line-by-line */
char *p; int o;
while(1){
fgets(in,501,stdin); /* takes line from user */
if((p=strstr(in,"/end/"))!=0){
/* if line has "/end/", all chars till /end/ are written to file and input loop breaks */
o = p-in;
fprintf(write,"%.*s",o,in);
break;
}
else{
/* writes line to file */
fputs(in,write);
}
}
if(ctrl==0){
/* in some cases, file must not be closed yet , hence ctrl is taken */
int s = fclose(write);
if(s==EOF) printf("\n Error ");
else printf("\n Success.\n");
}
}
void eat() /* clears stdin */
{
int eat;while ((eat = getchar()) != '\n' && eat != EOF);
}
int main(){
/* main to add/insert to file @ given index */
char fadd[501];/* filename str */
printf("\n Filename : "); scanf("%500[^\n]",fadd);
FILE * add = fopen(fadd,"r");
if(add==NULL)
perror("\n Error "); /* if file does not pre-exist */
else{
long long line, col; char sep; printf("\n Index : "); scanf("%lld%c%lld",&line,&sep,&col); /* takes index in ln-char-col form */
eat(); /* clears stdin */
FILE * tmp=fopen("Temp.Ctt","w"); /* opens a tmp file to write */
if(tmp==NULL)
perror("\n Error "); /* failed - tmp file could not be created to write */
else{
int f; /* success var */
if(line==1 && col>1){
/* if user wants to insert @ a col>1 in line 1 */
f = chkncpy(add,tmp,1,1,line,col); /* all below calls intend to write till 1 char before the char @ given index */
}
else if(line>1 && col>1){
/* if user wants to insert @ a col>1 in any line>1*/
f = chkncpy(add,tmp,1,1,line,col-1);
}
else if(line>1 && col==1){ //ERRORS - ignores the '\n' of line-1
/* if user wants to insert @ a 1st col in line>1 */
f = chkncpy(add,tmp,1,1,line-1,lcc(add,line-1));
}
else if(line==1 && col==1){
/* if user wants to insert @ 1,1 (no moving/copying needed) */
f=0;
}
else{
printf("\n Invalid Index.\n");f=-1;
}
if(f==0){
writer(tmp,1);
/* calls function to allow user to write to fptr , with ctrl != 0, so writer() *won't* fclose(tmp) */
chkncpy(add,tmp,line,col,lncnt(add),lastlncol(add));
/* copies all characters from index till EOF */
int ok = fclose(tmp); fclose(add);
if(ok==EOF){
/* if closing tmp was unsuccessful, the file on disk may be corrupted/incomplete, so must be removed */
remove("Temp.Ctt");perror("\n Error ");
}
else{
if(rename("Temp.Ctt",fadd)==0)
printf("\n Success.\n");
else{
/* on Windows & some other non-POSIX systems, file cannot be renamed to pre-existing filename , hence delete original */
remove(fadd);
if(rename("Temp.Ctt",fadd)==0)
printf("\n Success.\n");
else{
/* if rename still unsuccessful, throw an error, remove tmp file and give up */
remove("Temp.Ctt");
perror("\n Error ");
}
}
}
}
}
}
}
如果有任何遗漏的细节或无意的错误,请发表评论,我会更正它们。
最佳答案
你的代码太复杂了:
复制文件开头的不同情况应该合并到一个函数调用中,该函数调用将数据复制到并排除 col
行 line< 上的字节
.
然后您从用户输入中复制。
最后复制输入文件的其余部分。
代码假定行和列从 1
开始编号。这可能是显而易见的,但您应该具体说明这一点,因为它可能对每个人来说都不是显而易见的。
这种风格很难阅读:你应该在二元运算符周围使用水平空间,在关键字、、
和 ;
之后和 {< 之前
。在一行中塞满多个语句也不是一个好主意。
这是一个简化版本。通常,大部分代码用于错误处理:
#include <stdio.h>
#include <string.h>
void flush_line(FILE *fp) { /* read the rest of the current line */
int c;
while ((c = getc(fp)) != '\n' && c != EOF)
continue;
}
int copy_file(FILE *from, FILE *to) {
int c;
while ((c = getc(from)) != EOF) {
if (putc(c, to) == EOF)
return EOF;
}
return 0;
}
int copy_lines(FILE *from, int line1, int col1, FILE *to) {
int c, line = 1, col = 1;
for (;;) {
if (line >= line1 || (line == line1 && col >= col1))
break;
if ((c = getc(from)) == EOF)
break;
if (putc(c, to) == EOF)
return EOF;
col++;
if (c == '\n') {
line++;
col = 1;
}
}
return 0;
}
int writer(FILE *write) {
char in[501]; /* str that stores input line-by-line */
char *p;
printf("\n Terminate Input with \"/end/\".\n\n Type below :\n\n");
while (fgets(in, 501, stdin)) {
if ((p = strstr(in, "/end/")) != NULL) {
/* if line has "/end/", all chars till /end/ are written to file and input loop breaks */
int o = p - in;
if (fprintf(write, "%.*s", o, in) < 0)
return EOF;
break;
} else {
/* writes line to file */
if (fputs(in, write) < 0)
return EOF;
}
}
return 0;
}
int main() {
/* main to add/insert to file @ given index */
const char *temp_filename = "Temp.Ctt";
char fadd[501];/* filename str */
printf("\n Filename : ");
if (scanf("%500[^\n]", fadd) != 1)
return 1;
flush_line(stdin);
FILE *add = fopen(fadd, "r");
if (add == NULL) {
perror("\n Cannot open input file");
return 1;
}
int line, col;
/* read the index: 1 based line and column numbers */
printf("\n Index : ");
if (scanf("%d%*c%d", &line, &col) != 2) {
fprintf(stderr, "invalid input\n");
return 1;
}
flush_line(stdin);
FILE *tmp = fopen(temp_filename, "w");
if (tmp == NULL) {
perror("\n Cannot create temporary file");
fclose(add);
return 1;
}
if (copy_lines(add, line, col, tmp)) {
perror("\n Error copying beginning of file");
fclose(add);
fclose(tmp);
remove(temp_filename);
return 1;
}
if (writer(tmp)) {
perror("\n Error writing user input");
fclose(add);
fclose(tmp);
remove(temp_filename);
return 1;
}
if (copy_file(add, tmp)) {
perror("\n Error copying remaining file contents");
fclose(add);
fclose(tmp);
remove(temp_filename);
return 1;
}
fclose(add);
if (fclose(tmp) < 0) {
/* if closing tmp was unsuccessful, the file on disk may be corrupted/incomplete, so must be removed */
perror("\n Error closing temporary file");
remove(temp_filename);
return 1;
}
if (rename(temp_filename, fadd) == 0) {
printf("\n Success.\n");
return 0;
}
/* on Windows & some other non-POSIX systems, file cannot be renamed to pre-existing filename , hence delete original */
if (remove(fadd)) {
perror("\n Cannot remove input file");
remove(temp_filename);
return 1;
}
if (rename(temp_filename, fadd) == 0) {
printf("\n Success.\n");
return 1;
}
/* if rename still unsuccessful, try and copy contents */
tmp = fopen(temp_filename, "r");
if (tmp == NULL) {
perror("\n Cannot re-open temporary file, output is in Temp.Ctt");
return 1;
}
add = fopen(fadd, "w");
if (add == NULL) {
perror("\n Cannot re-open input file, output is in Temp.Ctt");
fclose(tmp);
return 1;
}
if (copy_file(tmp, add)) {
perror("\n Error copying temporary file to input file");
fclose(add);
fclose(tmp);
return 1;
}
fclose(tmp);
if (fclose(add) < 0) {
perror("\n Error closing input file");
return 1;
}
/* throw an error, remove tmp file and give up */
remove(temp_filename);
printf("\n Success.\n");
return 0;
}
关于c - 尝试将 'insert' 或 'add' 写入文本文件 - 一个小问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63756638/
我正在开发一些用于计费的数据库项目(PHP/MySQL)。 每当创建新账单时,我想生成一个由年、周和增量编号组成的账单编号。我想用触发器来做到这一点。触发器将使用现有的账单编号来查找增量编号,或者从新
我有一个 MySQL 插入,我正在使用 RAND 生成随机 INT 值问题是它不会插入到数据库中,因为该列接受 TINYINT ,如何将输出转换为 TINYINT。代码示例如下: INSERT INT
如果我想从单个插入中保存主键 (mytable_id),我已完成以下操作: CREATE OR REPLACE FUNCTION myfunct(ownerid text) RETURNS void
为了简单起见,假设我有两个表 用户表(id,email) 用户日志表(id, date) 无论 id 被插入到 user 表中,相同的 id 也应该被插入到 user_log 表中,否则事务应该失败。
为了简单起见,假设我有两个表 用户表(id,email) 用户日志表(id, date) 无论 id 被插入到 user 表中,相同的 id 也应该被插入到 user_log 表中,否则事务应该失败。
我知道在触发器中 - 至少对于 SQL Server - 人们永远不应该假设插入的表只有一行,这意味着触发器中这样的 SQL 通常是不好的: select @UserID = ID from inse
我正在使用 bigquery 对象中的方法 tabledata().insertAll 更新行列表。执行后,返回显示没有错误。但是,我的表仍然继续,没有写入任何数据。 可能是权限问题。如果是这样,为什
这是一个扩展 F# Recursive Tree Validation 的问题,我昨天已经很好地回答了。 这个问题涉及在现有树中插入一个 child 。这是我想使用的更新类型: type Name
我有 2 个表:用户和照片(在 mysql 数据库中)。 在这里你可以看到两个表之间的关系 User Photos -------------
我试图同时在不同的表上插入两行。 子查询INSERT INTO的AUTO_INCRMENT或id的值(如果已经存在)应该写入主查询中。 目前我有这个(仅用 3 个值简化),但它不起作用。我想知道是否有
我有一个 900 万行的表,由于其庞大的规模,我正在努力处理所有这些数据。 我想做的是在不覆盖数据的情况下将 IMPORT 一个 CSV 添加到表中。 在我做这样的事情之前; INSERT if no
我正在写新闻并将其插入到我的数据库中,我在 3 年前构建了代码并且运行良好,但我不能再插入了,我不明白为什么: $insert=mysqli_query($co,"INSERT INTO articl
我正在尝试编写一个简单的 INSERT 语句来将新用户添加到数据库中,但它不起作用,这意味着,我尝试插入到表中的数据都没有被插入。几个小时以来,我一直在尝试解决此问题,但没有成功。我尝试编写插入语句的
所以我有这个表格: http://i.imgur.com/vZYssQy.png 现在 ID、First Name、Last Name、DOB、Address、Phone Number 和 Post
在控制台中运行查询(SELECT 语句)时,从数据库检索到的数据以表格格式显示在数据库控制台工具窗口的结果 Pane 中。 我已经搜索过 datagrip Help我只是想知道是否有任何方法可以用于为
每当使用触发器插入行时,我都试图将另一行插入表中,但收到以下错误消息: The target table 'EDDSDBO.Redaction' of the DML statement cannot
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 6 年前。 Improve
我有以下代码片段: $get_data = mysqli_query ($connect, "SELECT * FROM users WHERE username = '$username'");
情况:需要向 SQLite 数据库中插入大量数据。 问题:我们可以使用两个语句来插入数据 - data = [("111", "222", "333"), ("AAA", "BBB", "CCC"),
我的数据库中有一个表 Teacher: TABLE Teacher ( ID CHAR (7) NOT NULL , name
我是一名优秀的程序员,十分优秀!