标签: error-handling

C++23 `print` 是否检查写入是否成功进入流?

我想知道标准委员会是否已经修复了臭名昭著的Hello, world! 漏洞。我主要谈论新<print>库(尚未在任何编译器中使用)。

{fmt}库(它启发了标准库)尚未修复此问题。显然,它在输出时不会抛出任何异常/dev/full(从 v9.1.0 开始)。因此,使用 CI/O 函数(例如std::fflush错误处理)仍然是一件事。

下面的程序注意到错误并返回失败代码(因此没有错误):

#include <exception>
#include <cstdio>
#include <cstdlib>
#include <fmt/core.h>


int main()
{
    fmt::println( stdout, "Hello, world!" );
    if ( std::fflush( stdout ) != 0 || std::ferror( stdout ) != 0 ) [[unlikely]]
    {
        return EXIT_FAILURE;
    }
}
Run Code Online (Sandbox Code Playgroud)

但这在 C++23 中可能吗?

#include <print>
#include <exception>
#include <cstdio>
#include <cstdlib>


int main()
{
    try
    {
        std::println( stdout, "Hello, world!" );
    }
    catch ( const std::exception& …
Run Code Online (Sandbox Code Playgroud)

c++ io error-handling fmt c++23

-5
推荐指数
1
解决办法
576
查看次数

错误运算符'=='无法应用于'char'和'string'类型的操作数

我收到下面的错误,我不知道如何解决它.我只是想学习,但不能想出这个.我尝试用等号(=)替换它,并且有很多东西但没有用,代码是:

if (keyinfo.KeyChar == "a") 
{

}
Run Code Online (Sandbox Code Playgroud)

这是错误:

0019运算符'=='不能应用于'char'和'string'类型的操作数

如何避免出现此错误?

c# string error-handling equals char

-6
推荐指数
1
解决办法
871
查看次数

类型错误:“函数”对象不可迭代“Python 3

我正在尝试编写一个程序,该程序将从网络上打开一个包含 10,000 个单词的文本文件,然后“清理”该文件以去除无意义的单词,例如“aa”。我最终想用这些词来做其他事情,所以我想将非“无意义”的词添加到新列表中。每次我尝试运行它时,我都会遇到错误代码TypeError: 'function' object is not iterable

import urllib.request  

def readWordList():  

response = urllib.request.urlopen("http://www.mit.edu/~ecprice/wordlist.10000")
html = response.read()
data = html.decode('utf-8').split()

return data

clean = readWordList() 

def clean(aList):   
    newList = []
    for word in aList: 
        if range(len(word)) >= 2:
            newList.append(word)
    return newList


clean(clean)
Run Code Online (Sandbox Code Playgroud)

python error-handling file python-3.x

-6
推荐指数
1
解决办法
5399
查看次数

令人困惑的python - 无法将字符串转换为float

我有一个值错误,即使我尝试使用代码,它也不起作用!

我该怎么做对吗? - 我正在使用Python 3.3.2!

这是代码: 码

如您所见,该程序会询问您可以行走多少英里,并根据您输入的内容为您提供响应.

这是文本格式的代码:

print("Welcome to Healthometer, powered by Python...")
miles = input("How many miles can you walk?: ")
if float(miles) <= 0:
    print("Who do you think you are?!! Go and walk 1000 miles now!")
elif float(miles) >= 10:
    print("You are very healthy! Keep it up!")
elif float(miles) > 0 and miles < 10:
    print("Good. Try doing 10 miles")
else:
    print("Please type in a number!")
    miles = float(input("How many miles can you walk?: "))
    if miles …
Run Code Online (Sandbox Code Playgroud)

python error-handling

-8
推荐指数
1
解决办法
6万
查看次数

未在此范围内声明(C++中的数组)

我是C++编程的初学者我有一个文本文件,其中有100万个素数用空格分隔.我想把它们放在一个int数组primes []中.以下是我写的代码:

int main()
{
    ifstream infile;
    infile.open("C:/Users/DELL/Desktop/primes1.txt");

    //check for error
    if(infile.fail()){cerr<<"Error Opening File"<<endl;
    exit(1);}
    int i=0;
    primes = new int[1000001];
    while(i != infile.eof()){
        infile>>primes[i];
        i++;
    }

    cout<<  primes[4]  <<endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我构建并运行时,它会出现以下错误:

"错误:'primes'未在此范围内声明"

这是什么解决方案?

c++ arrays error-handling text file

-8
推荐指数
2
解决办法
454
查看次数

标签 统计

error-handling ×5

c++ ×2

file ×2

python ×2

arrays ×1

c# ×1

c++23 ×1

char ×1

equals ×1

fmt ×1

io ×1

python-3.x ×1

string ×1

text ×1