- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试编写一个程序,在一个字符和另一个字符之间剪切文本文件。为了说明这一点,我想要剪切的 example.txt
包含数字和一些字符,例如 '[[['
和 ]]]
(它们可能会因包机的类型和数量而异)。这些字符标记切割过程的开始或结束。
这个过程看起来很简单;然而,这可能会很累,而且有风险,很可能会犯错误。不管怎样,当我写问题时,除了保存到另一个文本文件之外,我解决了很多关于我的问题的问题。 [[[
的计数是不确定的:也就是说,我不知道我要创建多少个文件。我对新思想和想法持开放态度,无论是哪种编程语言。
示例.txt
005076,-0.00008389,0.00039446,-0.00007079,0.00024138,0.00059449,-0.000 24860,[[[ 0.00032513,0.00000142,0.00001302,0.00000965,0.00005201,0.00001 395,-0.00003729,-0.00001774,-0.00002842,0.00000745,0.00143565,-0.00014 975,0.00166676,0.00086413,-0.00071107,-0.00081336,-0.10930688,0.062458 9,-0.00000142,0.00001301,0.00000965,-0.00005203,-0.00001394,-0.0000373 0,0.00001775,0.00002840,0.00000747,0.00143637,0.00015079,0.00166945 ]]] 0.00086406,0.00071132,-0.00081439,0.10930956,-0.06242029,-0.21353303,0 00003668,-0.00000001,0.00088168,-0.03589109,-0.00000584,0.00366844,0.0 [[[ 0.3708,-0.00000623,-0.00000093,-0.00001597,-0.00010373,-0.00001235,- 0.00008982,0.00008547,0.00000138,0.00001692,-0.00005915,0.00000005,0.0 090,0.00000182,-0.00000396,-0.00000575,0.00000367,0.00000364,-0.000012 73,0.00004112,-0.00000056,0.00007256,0.00001237,-0.00000176 ]]]
cut1.txt
0.00032513,0.00000142,0.00001302,0.00000965,0.00005201,0.00001 395,-0.00003729,-0.00001774,-0.00002842,0.00000745,0.00143565,-0.00014 975,0.00166676,0.00086413,-0.00071107,-0.00081336,-0.10930688,0.062458 9,-0.00000142,0.00001301,0.00000965,-0.00005203,-0.00001394,-0.0000373 0,0.00001775,0.00002840,0.00000747,0.00143637,0.00015079,0.00166945
cut2.txt
0.3708,-0.00000623,-0.00000093,-0.00001597,-0.00010373,-0.00001235,- 0.00008982,0.00008547,0.00000138,0.00001692,-0.00005915,0.00000005,0.0 090,0.00000182,-0.00000396,-0.00000575,0.00000367,0.00000364,-0.000012 73,0.00004112,-0.00000056,0.00007256,0.00001237,-0.00000176
$ cygcheck --version
cygcheck (cygwin) 2.0.4
System Checker for Cygwin
Copyright (C) 1998 - 2015 Red Hat, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ gcc --version
gcc (GCC) 4.9.2
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ gcc -o cutt cutt.c
$ ./cutt
0.00032513,0.00000142,0.00001302,0.00000965,0.00005201,0.00001 395,-0.00003729,-0.00001774,-0.00002842,0.00000745,0.00143565,-0.00014 975,0.00166676,0.00086413,-0.00071107,-0.00081336,-0.10930688,0.062458 9,-0.00000142,0.00001301,0.00000965,-0.00005203,-0.00001394,-0.0000373 0,0.00001775,0.00002840,0.00000747,0.00143637,0.00015079,0.00166945 ]][ 0.3708,-0.00000623,-0.00000093,-0.00001597,-0.00010373,-0.00001235,- 0.00008982,0.00008547,0.00000138,0.00001692,-0.00005915,0.00000005,0.0 090,0.00000182,-0.00000396,-0.00000575,0.00000367,0.00000364,-0.000012 73,0.00004112,-0.00000056,0.00007256,0.00001237,-0.00000176
如果是,有多少个 [[[
和 ]]]
v cutN.txt
文件。需要。
#include<stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX_LEN 1024
int testp=0, r=0;
char cx[MAX_LEN],cz[MAX_LEN];
void Cutt(char[],char[],int);
main()
{
FILE *fpR, *fpW;
char ch[MAX_LEN],sEArch4[3];
int chc,i=0,cxc;
sEArch4[1]=0x5b;sEArch4[2]=0x5b;sEArch4[3]=0x5b;
fpR = fopen("example.txt","r");
if (fpR==NULL) {
printf("Could not open example.txt !\n");
return 1;
}
fpW = fopen("cut1.txt","w");
if (fpW==NULL) {
printf("Could not open cut1.txt!\n");
return 1;
}
while(fgets(ch,MAX_LEN,fpR)) {
//printf("%s",ch);
chc = strlen(ch);
for(i=0;i<chc;i++)
//printf("%s %d %d",ch,i,chc);
Cutt(ch,sEArch4,i);
}
//printf("%s",cx);
cxc = strlen(cx);
for(i=1;i<cxc-2;i++)
cz[i-1] = cx[i];
printf("%s",cz);
//Jump;
return 0;
}
void Cutt(char ch[],char sEArch4[],int i)
{
int j,k=0;
for (j=i ; j<i+strlen(sEArch4) ; j++ ) {
if(ch[j]!=sEArch4[k]){
if ( ch [j] == 0x5b && ch [j-1] == 0x5b && ch [j-2] == 0x5b ) {
testp = 1;
}
if ( ch [j] == 0x5d && ch [j-1] == 0x5d && ch [j-2] == 0x5d ) {
testp = 2;
}
if(testp==1) {
cx[r] = ch[j];
r++;
return;
}
//printf("testp:%d \nch:%s\n",testp,x);
if(testp==2)
return;
//goto Jump;
else
return;
k++;
}
}
}
注意:对于单次切割,它运行没有问题。对于多次剪切,有 ]][
字符。查看编译信息。我认为它将用于剪切。
最佳答案
有几种方法可以解决这个问题。一种是像您一样逐行阅读,另一种是逐个字符地阅读。逐个字符的方法有一些优点,但主要优点是,如果一次处理一个字符,则不必担心特殊情况,例如包含以下内容的单行:
0.345[[[0.456]]]0.001[[[0.002]]]0.999[[[
如果你一次扫描一行,你必须相当小心地处理这些困难的数据。通过复制问题中的数据并不完全清楚它是否全部在一行上。此外,代码必须准确处理 [[
或 [
后跟 [
之外的其他内容,以及与 ]]< 类似的处理
或 ]
后跟 ]
以外的内容。
您似乎没有每次识别 [[[
序列时打开不同文件的代码;您也没有明确关闭辅助文件的代码。据推测,如果 ]]]
序列之前的最后一个字符不是换行符,则代码应输出换行符,以便文本文件实际上以换行符结尾。
通用命令将具有参数来指定打开序列、关闭序列、输出文件名的前缀(可能还有后缀),并且将读取命令行上指定的文件,或者默认为标准输入(如果没有)文件已指定。出于本练习的目的,我们将保留所有内容的硬连线,但从长远来看,这不是应该这样做的方式。
这就是我要做的。提交它作为答案时要小心——它会做一些你可能没有想到的事情。
#include <stdio.h>
#include <stdlib.h>
static int filenum = 0;
static const char s_mark[] = "[[[";
static const char e_mark[] = "]]]";
static const char f_prefix[] = "cut";
static const char f_suffix[] = ".txt";
static const char datafile[] = "example.txt";
static FILE *open_file(void)
{
char filename[32];
snprintf(filename, sizeof(filename), "%s%d%s", f_prefix, ++filenum, f_suffix);
FILE *fp = fopen(filename, "w");
if (fp == 0)
{
fprintf(stderr, "Failed to create file %s for writing\n", filename);
exit(EXIT_FAILURE);
}
return fp;
}
static void dump_to_file(FILE *ifp)
{
FILE *ofp = open_file();
int ch;
char last = '\0';
int e_mark_idx = 0;
while ((ch = getc(ifp)) != EOF)
{
if (ch == e_mark[e_mark_idx])
{
e_mark_idx++;
if (e_mark[e_mark_idx] == '\0')
{
/* All done */
if (last != '\n')
putc('\n', ofp);
break;
}
}
else
{
if (e_mark_idx > 0)
fprintf(ofp, "%.*s", e_mark_idx, e_mark);
e_mark_idx = 0;
putc(ch, ofp);
last = ch;
}
}
fclose(ofp);
}
int main(void)
{
int s_mark_idx = 0;
int ch;
FILE *ifp = fopen(datafile, "r");
if (ifp == 0)
{
fprintf(stderr, "Failed to open file %s for reading\n", datafile);
exit(EXIT_FAILURE);
}
while ((ch = getc(ifp)) != EOF)
{
if (ch == s_mark[s_mark_idx])
{
s_mark_idx++;
if (s_mark[s_mark_idx] == '\0')
{
/* Found start marker - dup following text to file */
dump_to_file(ifp);
s_mark_idx = 0;
}
}
else
{
if (s_mark_idx > 0)
printf("%.*s", s_mark_idx, s_mark);
s_mark_idx = 0;
putchar(ch);
}
}
fclose(ifp);
return 0;
}
给定此示例输入文件:
005076,-0.00008389,0.00039446,-0.00007079,0.00024138,0.00059449,-0.000 24860,[[[ 0.00032513,0.00000142,0.00001302,0.00000965,0.00005201,0.00001 395,-0.00003729,-0.00001774,-0.00002842,0.00000745,0.00143565,-0.00014 975,0.00166676,0.00086413,-0.00071107,-0.00081336,-0.10930688,0.062458 9,-0.00000142,0.00001301,0.00000965,-0.00005203,-0.00001394,-0.0000373 0,0.00001775,0.00002840,0.00000747,0.00143637,0.00015079,0.00166945 ]]] 0.00086406,0.00071132,-0.00081439,0.10930956,-0.06242029,-0.21353303,0 00003668,-0.00000001,0.00088168,-0.03589109,-0.00000584,0.00366844,0.0 [[[ 0.3708,-0.00000623,-0.00000093,-0.00001597,-0.00010373,-0.00001235,- 0.00008982,0.00008547,0.00000138,0.00001692,-0.00005915,0.00000005,0.0 090,0.00000182,-0.00000396,-0.00000575,0.00000367,0.00000364,-0.000012 73,0.00004112,-0.00000056,0.00007256,0.00001237,-0.00000176 ]]]
0.345[[[0.456]]]0.001[[[0.002]]]0.999[[[
0.2333]]]
[[0.234[234.567]]]0.000[[[0.123]0.234]]0.345]]]
它生成标准输出:
005076,-0.00008389,0.00039446,-0.00007079,0.00024138,0.00059449,-0.000 24860, 0.00086406,0.00071132,-0.00081439,0.10930956,-0.06242029,-0.21353303,0 00003668,-0.00000001,0.00088168,-0.03589109,-0.00000584,0.00366844,0.0
0.3450.0010.999
[[0.234[234.567]]]0.000
并将 cut1.txt
文件写入 cut6.txt
:
cut1.txt: 0.00032513,0.00000142,0.00001302,0.00000965,0.00005201,0.00001 395,-0.00003729,-0.00001774,-0.00002842,0.00000745,0.00143565,-0.00014 975,0.00166676,0.00086413,-0.00071107,-0.00081336,-0.10930688,0.062458 9,-0.00000142,0.00001301,0.00000965,-0.00005203,-0.00001394,-0.0000373 0,0.00001775,0.00002840,0.00000747,0.00143637,0.00015079,0.00166945
cut2.txt: 0.3708,-0.00000623,-0.00000093,-0.00001597,-0.00010373,-0.00001235,- 0.00008982,0.00008547,0.00000138,0.00001692,-0.00005915,0.00000005,0.0 090,0.00000182,-0.00000396,-0.00000575,0.00000367,0.00000364,-0.000012 73,0.00004112,-0.00000056,0.00007256,0.00001237,-0.00000176
cut3.txt:0.456
cut4.txt:0.002
cut5.txt:
cut5.txt:0.2333
cut6.txt:0.123]0.234]]0.345
关于c - 在 C 中,我应该如何在字符和另一个字符之间剪切文本文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31307441/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!