我正在研究MediaPlayer项目,该项目将播放文本文件而不是音频或视频。我的问题是如何显示C字符串数组中的字符串?例如,如果在文本文件中我有(不带引号):将文本文件读入c字符串
**"This is only test"**
和我的节目,我想在像一次显示一个字:
This
is
only
test
我怎么做?这是我到目前为止的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Global variables
int position=0; //to set the player position
float rate = 1.0; //the rate of playback
void LoadFile(const char *fileName, char *text){
FILE *ifp; //a file pointer for file read/write
//opening the file
ifp = fopen(fileName, "r");
if(ifp == NULL){
fprintf(stderr, "Can't open input file \"%s\"!\n",fileName);
exit(1);
}
else{
fgets(text,50,ifp); //getting the text from the file
}
//closing the file
fclose(ifp);
}
void *Start();
void *Stop();
void Rewind();
void SeekTo(int byteOffset);
void setRate(float rate);
int main(void){
int i = 0;
char choice;
char fileName[25];
char lineOfText[50]; //holds the text inside the file
printf("Please enter the file you want to open: ");
scanf("%s", fileName);
LoadFile(fileName,lineOfText);
printf("%s",lineOfText); //I will display the whole string
return 0;
}
如果你已经标记了你的问题“C++”,你为什么要使用C函数和原始数组? –
'人perror','人strerror'为有用的错误信息。 –
您可能想要修改这个问题:它不是真的关于“如何将文本文件读入字符串”,而是更多“如何枚举字符串中的每个单词”。 – Mordachai