C++删除数组中的最后一个符号
我遇到了一个问题。我有一个代码应该读取以下文本文件,删除最后一个符号并在其他文件中输出新文本。我的问题是:如何删除最后一个符号?我尝试过类似的事情
for(int j=0;j<10000;j++)
{
if(simbols[j]=='\0')
{
cout<<j;
minusOne = j-1;
minusOne ='\0';
break;
}
}
,但它并没有真正帮助并做我想做的任何事情。任何帮助将不胜感激!
聚苯乙烯 如果有任何帮助,这里是完整的代码:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char simbols[10000]; int minusOne;
ofstream outFile("outFile.out");
ifstream inFile("inFile.in");
for(int i=0;i<10000;i++)
{
inFile >> simbols[i];
}
for(int j=0;j<10000;j++)
{
if(simbols[j]=='\0')
{
cout<<j;
minusOne = j-1;
minusOne ='\0';
break;
}
}
if(outFile.is_open())
{
for(int l=0;l<10000;l++)
{
outFile << simbols[l];
}
}
inFile.close();
outFile.close();
return 0;
}
I've encountered a problem. I have a code which should read the following text file, delete the last symbol and output the new text in other file. My problem would be: how to delete the last symbol? I've tried things like
for(int j=0;j<10000;j++)
{
if(simbols[j]=='\0')
{
cout<<j;
minusOne = j-1;
minusOne ='\0';
break;
}
}
But it doesn't really help and do anything I want. Any help would be appreciated!
P.S.
If it would be any help here's the full code:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char simbols[10000]; int minusOne;
ofstream outFile("outFile.out");
ifstream inFile("inFile.in");
for(int i=0;i<10000;i++)
{
inFile >> simbols[i];
}
for(int j=0;j<10000;j++)
{
if(simbols[j]=='\0')
{
cout<<j;
minusOne = j-1;
minusOne ='\0';
break;
}
}
if(outFile.is_open())
{
for(int l=0;l<10000;l++)
{
outFile << simbols[l];
}
}
inFile.close();
outFile.close();
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
minusOne
只是一个整型变量,与符号数组无关。您想让它成为一个指向char
的指针并指向数组的最后一个元素:char simbols[10000]; char* minusOne = simpols;
...
当然,最干净的方法就是 bert-jan 建议的方法。
PS 忍不住指出,正确的拼写是“symbol”,而不是“simbol”。
minusOne
is just an integer variable, unrelated to the array of symbols. You wanted to make it a pointer tochar
and point to the last element of array:char simbols[10000]; char* minusOne = simpols;
...
And, of course, the clean way of doing it is the way bert-jan has suggested.
P. S. Can't help but point out that the right spelling is "symbol", not "simbol".
这是删除字符串缓冲区的最后一个字符的方法:
This is how to delete the last character of a string buffer:
在我看来你的意思是放
而不是
It looks to me like you meant to put
instead of