Tag: 文件

不能在C中正确读取带有密码的.txt文件

对于我的代码,我需要创建一个包含用户名(名字)和密码(姓氏)的.txt文件。 我的代码需要读取该文件。 如果我输入了正确的用户名和密码,它将登录。如果不正确,则不会登录。到目前为止,在我的name.txt文件中(包含我的用户名和密码的文件)我有Lebron James,Joe Smith。 当我运行我的代码时,我只是在命令提示符下显示“无法打开文件names.txt”。 我想知道我需要对我的代码进行哪些更改? //This is my code: #include #include #include #define MAX_USER 32 void get_input(const char *pszPrompt, char *cpszWord, const size_t iLength) { while (1) { printf(“%s> “, pszPrompt); if (fgets(cpszWord, iLength * sizeof(char), stdin)) { break; } } size_t iLen = strlen(cpszWord); if (cpszWord[iLen – 1] == ‘\n’) cpszWord[iLen – […]

C程序:从文本文件中读取和打印特定列时出错

我想将第6列从文本文件“1PGB.pdb”打印到输出文件“out”。 以下是我的代码: #include #include /* For exit() function*/ #define ROW 500 #define COL 12 int main() { int n6, garbage; FILE *fp; FILE *op = fopen(“out”, “w”); int score[ROW][COL]; int row, col, item, i, j; if ((fp=fopen(“1PGB.pdb”,”r”))==NULL) { printf(“Error! opening file”); exit(1); /* Program exits if file pointer returns NULL. */ } for(row=0; row<ROW; row++) […]

当我们不向C可执行程序提供参数时如何处理exception

我有这个C程序,它应该给出如下参数: ./program -i inputFile -o outputFile 这是我相关的代码部分 while ((c = getopt(argc, argv, “i:o:”)) != -1) { switch (c) { case ‘i’: inFile = strdup(optarg); break; case ‘o’: outFile = strdup(optarg); break; default: error_usage(argv[0]); } } 这里也是error_usage函数: void error_usage(char *prog) { fprintf(stderr, “Usage: %s -i inputfile -o outputfile\n”, prog); exit(1); } 我应该如何修改我的case语句,如果我运行我的程序如下: ./program它会给我以下错误? Usage: prog -i […]

在C Win32中使用hBitmap = CreateDIBSection()保存.bmp文件

我有以下代码。 我想用这个信息创建一个.bmp文件来显示内存中的数据,这是一个从framegrabber复制到主内存的图像,有人可以让我来吗? 谢谢 LPSTR CreateBMP( HWND hAppWnd, int nImageType ) { void * pWinGBits = NULL; int i; Z_BITMAPINFO zWinGHeader; //用于终止DIB的bitmapinfo // create DC for bitmap. hDCBits = CreateCompatibleDC( ghDCMain ); switch ( nImageType ) { case bayer_filter: zWinGHeader.bmiHeader.biSize = sizeof( BITMAPINFOHEADER ); zWinGHeader.bmiHeader.biPlanes = 1; zWinGHeader.bmiHeader.biClrImportant = 0; zWinGHeader.bmiHeader.biHeight = -lYSize; zWinGHeader.bmiHeader.biWidth = lXSize; […]

从.txt文件中读取随机行

我正试图通过从.txt文件中读取随机单词来升级我的Hangman游戏。 事实是,我无法弄清楚如何从.txt文件中读取随机行。 .txt文件的每个新行都有单个单词。 void ler_palavras() { FILE *words; if ((words = fopen(“words.txt”, “r”)) == NULL) { printf(“Error! opening file”); exit(1); } // reads text until newline fscanf(words,”%[^\n]”, word); fclose(words); }

区分文件C中的数据

我试图读取文件的内容,这些文件被分成两个不同的类型。 如下所示: # Type names bird mammal reptile . # Type effectiveness Very_effective Not_effective . 到目前为止,我可以读取第一种类型的内容,但是当我尝试读取第二种内容时,我不断重读第一种内容。 #include #include #include int main() { typedef struct { char types[1000]; char effectiveness[1000]; } sinFile; sinFile record[1000]; FILE* file; char line[121]; char period[10]; char hash[10]; char* item; char* item2; int i = 0; int j = 0; file = […]

如何在C上将文件写入非特定用户的桌面?

我写了一个程序,我想让它写一个文件到桌面。 我想出了如何把它写到我的,但我希望它能够去任何人的桌面(窗口)。 有没有办法让路径名称更具动态性,以便它可以在任何人的桌面上运行? 这是一个示例: void add(void) { FILE *fp; fp = fopen(“C:\\Users\\Jones\\Desktop\\test.txt”, “w+”); float num1; float num2; float sum; printf(“Enter first number: “); num1 = getNum(); printf(“Enter second number: “); num2 = getNum(); sum = num1 + num2; printf(“%.1f + %.1f = %.1f\n”, num1, num2, sum); fprintf(fp, “Num1: %.1f\nNum2: %.1f\nSum: %.1f “, num1, num2, sum); […]

目录结构C ++

C:\Projects\Logs\RTC\MNH\Debug C:\Projects\Logs\FF 是否有一个表达式/字符串,可以说直到找到“Logs”并打开它? (假设你总是低于它) 相同的可执行文件在不同的时间用完“Debug”,“MNH”或“FF”,可执行文件应始终将其日志文件保存到“Logs”中。 没有引用整个路径C:\ Projects \ Logs会有什么表达式? 谢谢。

在C中提高文件IO的性能

在对它们执行堆操作之后,我需要逐个将大量的整数写入文件。 我试图将排序的文件合并到一个文件中。 截至目前,我正在每次操作后写入文件。 我使用min heap来合并文件。 我的问题是 – 执行文件写入时,每次写入文件或一次写入多块内存块时是否访问磁盘? 如果我将在大小为1024的数组中输出堆,或者可能更多,然后立即执行写操作,它会改善性能吗? 先感谢您。 编辑 – 将使用setbuffer()帮助? 我认为它应该在某种程度上有所帮助。

用C读取文件

我的朋友和我正在研究一个项目,我们需要从C中读取文件的输入。 该文件如下所示: 15 25 200 3 10 17.99 22.99 109.99 100 2 4 5.99 99.99 20.00 49.99 10 10 10 10 10 10 10 10 10 10 3.99 5.99 7.99 8.00 5.00 5.00 5.00 6.00 7.00 9.99 5 我需要逐行读取文件,并将每个值设置为不同的变量。 例如,第一行的第一个值必须设置为变量preSalePrices,第二个值为doorPrices,第三个为preSales。 我需要帮助弄清楚如何指定每行的值的数量。 例如,如何告诉程序在第一行获得三个值,而在第二行只获得两个值? 然后在第五行上有四个值,依此类推。 这是我的代码,但它只是崩溃: int main() { float preSalePrices, doorPrices; int preSales; FILE *fp; fp […]