找到 34423 篇文章 相关编程

使用标准 C/C++ 检查文件是否存在的最有效方法

Nishtha Thakur
更新于 2019-07-30 22:30:26

16K+ 浏览量

检查文件是否存在唯一的方法是尝试以读或写模式打开文件。以下是一个示例 - 在 C 中示例#include int main() {    /* 尝试打开文件以读取 */    FILE *file;    if (file = fopen("a.txt", "r")) {       fclose(file);       printf("文件存在");    } else {       printf("文件不存在");    } }输出文件存在在 C++ 中示例#include #include using namespace std; int main() {    /* 尝试打开文件以读取 */    ifstream ifile;    ifile.open("b.txt");    if(ifile) {       cout

使用 C++ ifstream 从文本文件读取整数

Smita Kapse
更新于 2019-07-30 22:30:26

6K+ 浏览量

以下是用 C++ ifstream 从文本文件读取整数的示例。示例#include #include using namespace std; int main() {    // 初始化数组大小    int arr[30];    ifstream is("a.txt");    int cnt= 0;    int x;    // 检查数组是否已满    while (cnt < arr[30] && is >> x)    // 并从文件读取整数    arr[cnt++] = x;    // 打印存储在数组中的整数    cout

如何在 C 中编写自己的头文件?

Anvi Jain
更新于 2019-07-30 22:30:26

1K+ 浏览量

在 C 中编写自己的头文件的步骤 - 键入代码并将其另存为“sub.h”。编写一个主程序“subtraction.c”,其中 - 包含新的头文件。写入“sub.h”代替 sub.h 头文件中的所有函数现在都可以使用了。直接调用函数 sub()。“subtraction.c”和“sub.h”都应该在同一个文件夹中。Sub.hint sub(int m, int n) {    return(m-n); }subtraction.c示例#include #include "sub.h" void main() {    int a= 7, b= 6, res;    res = sub(a, b);    printf("两个数字的差为:%d", res); }运行“subtraction.c”后的输出将为 - 输出两个数字的差为:1阅读更多

如何在 C++ 中将文件内容读取到 istringstream 中?

Nishtha Thakur
更新于 2019-07-30 22:30:26

1K+ 浏览量

这是一个 C++ 程序,用于将文件内容读取到 C++ 中的 isstringstream 中。示例#include #include #include using namespace std; int main() {    ifstream is("a.txt", ios::binary );    // 获取文件长度:    is.seekg (0, std::ios::end);    long length = is.tellg();    is.seekg (0, std::ios::beg);    // 分配内存:    char *buffer = new char [length];    // 将数据作为块读取:    is.read (buffer,length);    // 创建内存内容的字符串流    istringstream iss( string( buffer ) );    cout

如何在 C++ 中获取文件的大小?

Smita Kapse
更新于 2019-07-30 22:30:26

14K+ 浏览量

要在 C++ 中获取文件的大小,首先打开文件并将其定位到末尾。tell() 将告诉我们流的当前位置,这将是文件中的字节数。示例实时演示#include #include using namespace std; int main() {    ifstream in_file("a.txt", ios::binary);    in_file.seekg(0, ios::end);    int file_size = in_file.tellg();    cout

C++ 中以二进制和文本模式写入的文件之间的区别

Anvi Jain
更新于 2019-07-30 22:30:26

547 浏览量

文本模式二进制模式在文本模式下,执行各种字符转换,即“\r+\f”转换为“”在二进制模式下,不会执行此类转换。要写入文件:ofstream ofs (“file.txt”);或ofstream ofs;ofs.open(“file.txt”);要写入文件:ofstream ofs(“file.txt”, ios::binary);或ofstream ofs;ofs.open(“file.txt”, ios::binary);要在文件末尾添加文本:Ofstream ofs(“file.txt”, ios::app);或ofstream ofs;ofs.open(“file.txt”, ios::app);要在文件末尾添加文本:Ofstreamofs(“file.txt”, ios::app|ios::binary);或 ofstream ofs;ofs.open(“file.txt”, ios::app|ios::binary);要读取文件:ifstream in (“file.txt”);或ifstreamin ; in.open(“file.txt”);要读取文件:ifstream in (“file.txt”, ios::binary);或ifstream in ;in.open(“file.txt”, ios::binary);阅读更多

C++ 程序在 STL 中实现 LexicoGraphical_Compare

Nishtha Thakur
更新于 2019-07-30 22:30:26

82 浏览量

C++ 函数 std::algorithm::lexicographical_compare() 测试一个范围是否小于另一个范围。字典序比较通常用于按字母顺序对字典中的单词进行排序。声明模板 bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);算法开始    result = lexicographical_compare(v1.begin(), v1.end(), v2.begin(), v2.end())    if (result == true)       打印 v1 小于 v2    result = lexicographical_compare(v1.begin(), v1.end(), v2.begin(), v2.end())    if (result == false)       打印 v1 不小于 v2 结束示例实时演示#include #include #include #include using namespace std; int main(void) {    // 初始化 ... 阅读更多

如何在 C/C++ 中使用指针数组(锯齿状)?

Smita Kapse
更新于 2019-07-30 22:30:26

550 浏览量

让我们考虑以下示例,它使用一个包含 3 个整数的数组 - 在 C 中示例实时演示#include const int MAX = 3; int main () {    int var[] = {10, 100, 200};    int i;    for (i = 0; i < MAX; i++) {       printf("var[%d] 的值为 %d", i, var[i] );    }    return 0; }输出var[0] 的值为 10 var[1] 的值为 100 var[2] 的值为 200在 C++ 中示例实时演示#include using namespace std; const int MAX = 3; int main () {    int var[] = {10, 100, 200};    int i;    for (i = 0; i < MAX; i++) {       cout

C++ 程序为给定数量的边生成一个随机无向图

Anvi Jain
更新于 2019-07-30 22:30:26

358 浏览量

这是一个 C++ 程序,我们为给定的边“e”生成一个无向随机图。该算法基本上是在一个大型网络上实现的,该算法的时间复杂度为 O(log(n))。算法开始    函数 GenerateRandomGraphs(),以“e”作为参数列表中的边数。    初始化 i = 0    while(i < e)       edge[i][0] = rand()%N+1       edge[i][1] = rand()%N+1       增加 I;    对于 i = 0 到 N-1       初始化 count = 0       对于 j = 0 到 e-1         ... 阅读更多

C++ 程序执行基于局部性原理的搜索

Nishtha Thakur
更新于 2019-07-30 22:30:26

100 浏览量

基于局部性原理的搜索取决于内存访问模式数据元素的重新分配。这里使用线性搜索方法来搜索元素。算法开始    int find(int *intarray, int n, int item)    初始化 comparisons = 0    对于 i = 0 到 n-1       增加 comparisons    if(item == intarray[i])       打印元素及其索引    break    if(i == n-1)       打印元素未找到    返回 -1    打印总比较次数    对于 j = i 到 i>0       intarray[j] = intarray[j-1]       intarray[0] = ... 阅读更多

广告

© . All rights reserved.