C++删除数组中的最后一个符号

发布于 2024-12-17 22:31:50 字数 926 浏览 4 评论 0原文

我遇到了一个问题。我有一个代码应该读取以下文本文件,删除最后一个符号并在其他文件中输出新文本。我的问题是:如何删除最后一个符号?我尝试过类似的事情

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月如刀 2024-12-24 22:31:50

minusOne 只是一个整型变量,与符号数组无关。您想让它成为一个指向 char 的指针并指向数组的最后一个元素:

char simbols[10000]; char* minusOne = simpols;

...

 cout<<j;
 minusOne += j-1;
 minusOne ='\0';
 break;

当然,最干净的方法就是 bert-jan 建议的方法。

PS 忍不住指出,正确的拼写是“symbol”,而不是“simbol”。

minusOne is just an integer variable, unrelated to the array of symbols. You wanted to make it a pointer to char and point to the last element of array:

char simbols[10000]; char* minusOne = simpols;

...

 cout<<j;
 minusOne += j-1;
 minusOne ='\0';
 break;

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".

最好是你 2024-12-24 22:31:50

这是删除字符串缓冲区的最后一个字符的方法:

char *buffer = strdup("hello world");

buffer[strlen(buffer) - 1] = '\0';

This is how to delete the last character of a string buffer:

char *buffer = strdup("hello world");

buffer[strlen(buffer) - 1] = '\0';
顾冷 2024-12-24 22:31:50

在我看来你的意思是放

simbols[minusOne] = '\0';

而不是

minusOne = '\0';

It looks to me like you meant to put

simbols[minusOne] = '\0';

instead of

minusOne = '\0';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文