如何将文本从一个文件复制到另一个文件,然后将文本字符串的第一个字母转换为大写
How to copy text from one file to another and then turning the first letters of the text string into uppercase
我正在尝试构建一个程序,该程序将文本从一个.txt文件复制到另一个文件,然后获取文本中每个单词的第一个字母并将其切换为大写字母。到目前为止,我只设法复制了大写部分的文本,没有运气或想法。任何提示或帮助将不胜感激。这是我到目前为止所拥有的:
int main()
{
std::ifstream fin("source.txt");
std::ofstream fout("target.txt");
fout<<fin.rdbuf(); //sends the text string to the file "target.txt"
system("pause");
return 0;
}
试试这个,将文件内容带到一个字符串中,然后处理它,然后再次写入 traget 文件。
int main()
{
std::ifstream fin("source.txt");
std::ofstream fout("target.txt");
// get pointer to associated buffer object
std::filebuf* pbuf = fin.rdbuf();
// get file size using buffer's members
std::size_t size = pbuf->pubseekoff (0,fin.end,fin.in);
pbuf->pubseekpos (0,fin.in);
// allocate memory to contain file data
char* buffer=new char[size];
// get file data
pbuf->sgetn (buffer,size);
fin.close();
locale loc;
string fileBuffer = buffer;
stringstream ss;
for (std::string::size_type i=0; i<fileBuffer.length(); ++i){
if(i==0)
ss << toupper(fileBuffer[i],loc);
else if (isspace(c))
ss << fileBuffer[i] << toupper(fileBuffer[++i],loc);
else
ss << fileBuffer[i];
}
string outString = ss.str();
fout << outString;
fout.close();
}
而不是一次复制整个文件,您需要将其部分或全部读取到本地"buffer"变量中 - 也许使用 while (getline(in, my_string))
,然后您可以简单地沿着位置 0 或前面有非字母的string
大写字母迭代(您可以使用 std::isalpha
和 std::toupper
), 然后将string
流式传输到out
。 如果您尝试并遇到困难,请将您的新代码附加到问题中,有人一定会帮助您......
我认为
对于此复制,整个文件不会让您对其进行编辑。可以使用 get()
和 put()
一次处理一个字符的文件。然后弄清楚如何检测单词的开头并将其设置为大写:
像这样:
int main()
{
std::ifstream fin("source.txt");
std::ofstream fout("target.txt");
char c;
while(fin.get(c))
{
// figure out which chars are the start
// of words (previous char was a space)
// and then use std::toupper(c)
fout.put(c);
}
}
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
int main() {
FILE* fpin;
FILE* fpout;
int counter = 0;
char currentCharacter;
char previousCharacter=' ';
fpin = fopen("source.txt", "r"); /* open for reading */
if (fpin == NULL)
{
printf("Fail to open source.txt!n");
return 1;
}
fpout = fopen("target.txt", "w");/* open for writing */
if (fpout == NULL)
{
printf("Fail to open target.txt!n");
return 1;
}
/* read a character from source.txt until END */
while((currentCharacter = fgetc(fpin)) != EOF)
{
/* find first letter of word */
if(!isalpha(previousCharacter) && previousCharacter != '-' && isalpha(currentCharacter))
{
currentCharacter = toupper(currentCharacter); /* lowercase to uppercase */
counter++; /* count number of words */
}
fputc(currentCharacter, fpout); /* put a character to target.txt */
/* printf("%c",currentCharacter); */
previousCharacter = currentCharacter; /* reset previous character */
}
printf("nNumber of words = %dn", counter);
fclose(fpin); /* close source.txt */
fclose(fpout); /* close target.txt */
return 0;
}
相关文章:
- 文本文件中的单词链表
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 如何将内容数组写入文本文件?
- 无法通过空白将文本文件行分隔为矢量
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- C++将文本文件中的数据读取到结构数组中
- 在指针的帮助下,文本文件中单词的频率
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 将值从二维数组输出到文本文件
- 如何使用 c++ 实现并发文件/文本编辑?
- 我需要转换一些代码,以便它适用于输入和输出文件文本
- 通过套接字发送二进制文件.文本文件有效,其他文件无效
- 将大型数据文件拆分为多个小文件(文本格式)
- 如何读取输入文件(文本文件)并将输入验证为有效整数
- 从文本文件/文本流中读取Q字符串错误
- 从文本文件(文本解析器)C++中读取行和列的逻辑
- 如何将*.css文件(文本文件)的内容与附加信息存储在新文件中?
- 关于内存映射文件和使用大文件文本编辑器
- 如何在读取文件文本时跳过"n"
- 使用libzip从.zip获取文件(文本除外)