简体   繁体   English

如何将文本从一个文件复制到另一个文件,然后将文本字符串的第一个字母转换为大写

[英]How to copy text from one file to another and then turning the first letters of the text string into uppercase

I am trying to build a program that copies text from one .txt file to another and then takes the first letter of each word in the text and switches it to an uppercase letter.我正在尝试构建一个程序,将文本从一个 .txt 文件复制到另一个文件,然后将文本中每个单词的第一个字母转换为大写字母。 So far, I have only managed to copy the text with no luck or idea on the uppercase part.到目前为止,我只设法复制了大写部分没有运气或想法的文本。 Any tips or help would be greatly appreciated.任何提示或帮助将不胜感激。 This is what I have so far:这是我到目前为止:

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;
} 

Try this, Take the file content to a string, then process it, and again write to the traget file.试试这个,把文件内容变成一个字符串,然后处理它,再次写入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();

}

Instead of copying the entire file at once, you'll need to read part or all of it into a local "buffer" variable - perhaps using while (getline(in, my_string)) , then you can simply iterate along the string capitalising letters that are either in position 0 or preceeded by a non-letter (you can use std::isalpha and std::toupper ), then stream the string to out .不是一次复制整个文件,您需要将其部分或全部读入本地“缓冲区”变量 - 可能使用while (getline(in, my_string)) ,然后您可以简单地沿着大写字母的string进行迭代位于位置 0 或前面是非字母(您可以使用std::isalphastd::toupper ),然后将string流式传输到out If you have a go at that and get stuck, append your new code to the question and someone's sure to help you out....如果您尝试并遇到困难,请将您的新代码附加到问题中,有人肯定会帮助您....

I think for this copying the whole file is not going to let you edit it.我认为复制整个文件不会让你编辑它。 You can use get() and put() to process the file one character at a time.您可以使用get()put()处理一个字符的文件。 Then figure out how to detect the start of a word and make it uppercase:然后弄清楚如何检测单词的开头并使其大写:

Something like this:像这样的东西:

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 = %d\n", counter);
    fclose(fpin);   /* close source.txt */
    fclose(fpout);  /* close target.txt */
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM