从文件读取后输出不打印到标准输出,在 C 中使用 printf ("%s", <a char* variable>);
Output not printing to standard output after reading from a file, in C using printf ("%s", <a char* variable>);
在 Windows 的 C 程序中,我在创建文件后读取文件,但没有任何内容打印到屏幕上,这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void readfileforunit1(char *param1)
{
char *concat(const char *str1, const char *str2)
{
char *result;
__mingw_asprintf(&result, "%s%s", str1, str2);
return result;
}
char *exten = ".txt";
char *theFileName = concat(param1, exten);
char *strPtr;
FILE *fptr;
fptr = fopen(theFileName, "r");
fscanf(fptr, "%[^\n]", strPtr);
printf("Here is the text from the file using char* str:\n%s", strPtr); /*This is where the
problem is, it is supposed to print 123456, 555.23*/
}
void structTest(char *param1, double param2)
{
char *concat(const char *str1, const char *str2)
{
char *result;
__mingw_asprintf(&result, "%s%s", str1, str2);
return result;
}
typedef struct
{
char *doc;
double value;
} Unit;
Unit justThis = {param1, param2};
char *exten = ".txt";
char *theFileName = concat(justThis.doc, exten);
FILE *outPutFile = fopen(theFileName, "w");
fprintf(outPutFile, "%s,%f", justThis.doc, justThis.value);
fclose(outPutFile);
}
int main(int argc, char *argv[])
{
structTest("123456", 555.23);
readfileforunit1("123456");
}
我基本上创建了一个包含 Unit 结构实例信息的 .txt 文件(文件名是实例的文档值),然后我想读取它并将其打印到标准输出,在本例中是屏幕。它应该在屏幕上打印 123456,555.23。
问题来自
char * strPtr;
这使得一个没有分配的字符指针space。要从文件中读取,通常使用缓冲区。两种方式,
使用字符数组(正如@agrudge-amicus
评论中提到的
char strPtr[SIZE]
使用Malloc动态分配内存
char *strPtr;
strPtr = malloc(sizeof(char) * SIZE);
注意:根据你的文件定义SIZE。
在 Windows 的 C 程序中,我在创建文件后读取文件,但没有任何内容打印到屏幕上,这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void readfileforunit1(char *param1)
{
char *concat(const char *str1, const char *str2)
{
char *result;
__mingw_asprintf(&result, "%s%s", str1, str2);
return result;
}
char *exten = ".txt";
char *theFileName = concat(param1, exten);
char *strPtr;
FILE *fptr;
fptr = fopen(theFileName, "r");
fscanf(fptr, "%[^\n]", strPtr);
printf("Here is the text from the file using char* str:\n%s", strPtr); /*This is where the
problem is, it is supposed to print 123456, 555.23*/
}
void structTest(char *param1, double param2)
{
char *concat(const char *str1, const char *str2)
{
char *result;
__mingw_asprintf(&result, "%s%s", str1, str2);
return result;
}
typedef struct
{
char *doc;
double value;
} Unit;
Unit justThis = {param1, param2};
char *exten = ".txt";
char *theFileName = concat(justThis.doc, exten);
FILE *outPutFile = fopen(theFileName, "w");
fprintf(outPutFile, "%s,%f", justThis.doc, justThis.value);
fclose(outPutFile);
}
int main(int argc, char *argv[])
{
structTest("123456", 555.23);
readfileforunit1("123456");
}
我基本上创建了一个包含 Unit 结构实例信息的 .txt 文件(文件名是实例的文档值),然后我想读取它并将其打印到标准输出,在本例中是屏幕。它应该在屏幕上打印 123456,555.23。
问题来自
char * strPtr;
这使得一个没有分配的字符指针space。要从文件中读取,通常使用缓冲区。两种方式,
使用字符数组(正如@agrudge-amicus
评论中提到的char strPtr[SIZE]
使用Malloc动态分配内存
char *strPtr; strPtr = malloc(sizeof(char) * SIZE);
注意:根据你的文件定义SIZE。