【问题标题】:changing first letter to uppercase with fscanf and fseek使用 fscanf 和 fseek 将第一个字母更改为大写
【发布时间】:2017-06-09 12:41:12
【问题描述】:

我的程序将 .txt 文件中每个单词的首字母更改为大写。 我输入文件的地址。这个程序将一个单词保存为一个名为“word”的字符数组。它将数组的第一个单元格更改为大写。然后计算该单词的字母并移回该单词的第一个字母。然后它将新单词写入文件。 但它不能正常工作!!!

#include <iostream>
#include <stdio.h>
using namespace std;
int main ()
{
    int t=0, i=0,j=0;
    char word[5][20];
    FILE *f;
    char adres[20];
    cin >> adres;   //  K:\\t.txt

    f=fopen(adres,"r+");


    {
        t=ftell(f);
        cout << t<<"\n";

        fscanf(f,"%s",&word[i]);
        word[i][0]-=32;
        for (j=0;word[i][j]!=0;j++){}
        fseek(f,-j,SEEK_CUR);
        fprintf(f,"%s",word[i]);


        t=ftell(f);
        cout << t<<"\n";
    }

    i++;

    {       
        fscanf(f,"%s",&word[i]);
        word[i][0]-=32;
        for (j=0;word[i][j]!=0;j++){}
        fseek(f,-j,SEEK_CUR);
        fprintf(f,"%s",word[i]);



        t=ftell(f);
        cout << t<<"\n";

    }

    return 0;
}

文件如下:
你好,kami,你好吗
答案是:
你好 kaAmi 你好吗

【问题讨论】:

  • 文件有多大?如果不是太大,我只需将文件读入内存,然后在输出时进行相应的修改。
  • #ifdef __cplusplus#error wrong compiler#endif
  • 你为什么不用std::fstream
  • 不是大文件。但我们希望它也适用于大文件。
  • 你只是想把每个单词的第一个字母大写吗?

标签: c++ file scanf fseek


【解决方案1】:

我想,这就是你需要的。

#include<iostream>
#include<cstring>
#include<fstream>

using namespace std;
void readFile()
{
    string word;
    ifstream fin;
    ofstream fout;
    fin.open ("read.txt");
    fout.open("write.txt");
    if (!fin.is_open()) return;


    while (fin >> word)
    {
    if(word[0]>='a' && word[0]<='z')
        word[0]-=32;
        fout<< word << ' ';
    }


}

int main(){

    readFile();
    return 0;
}

【讨论】:

  • 很抱歉,但我想知道我的程序出了什么问题。如果可以,请帮助我。@MIRMIX
  • 为什么要写这个词[i][j]!=0。写错了。
【解决方案2】:

这看起来像家庭作业。

  • 不要尝试在同一个文件中读写。使用不同的文件(例如in.txtout.txt)。您可以在最后删除和重命名文件。
  • 使用 c++ 流。
  • 一次读取一个字符。
  • 将您的算法分为三个部分:
    • 读取和写入空白,直到找到非空白字符。
    • 将字符改为大写并写。
    • 阅读和书写单词的其余部分。

这是一个起点:

#include <fstream>
#include <locale>

int main()
{
  using namespace std;

  ifstream is( "d:\\temp\\in.txt" );
  if ( !is )
    return -1;

  ofstream os( "d:\\temp\\out.txt" );
  if ( !os )
    return -2;

  while ( is )
  {
    char c;

    while ( is.get( c ) && isspace( c, locale() ) )
      os.put( c );
    is.putback( c );

    // fill in the blanks

  }

  return 0;
}

[编辑] 你的程序有太多问题。

  • 不清楚您要做什么。您可能希望每个单词都大写。
  • scanf 函数跳过字符串前面的空格。如果文件包含“abc”(注意“a”前面的空格)并且您使用fscanf,您将得到“abc” - 没有空格。
  • 从字符中减去 32 不一定会将其转换为大写字母。如果是数字而不是字母怎么办?相反,您应该使用toupper 函数。

[编辑]一个文件和c样式:

#include <stdio.h>
#include <ctype.h>

int main()
{
  FILE* f = fopen( "d:\\temp\\inout.txt", "r+b" );
  if ( !f )
    return -1;

  while ( 1 )
  {
    int c;

    //
    while ( ( c = getc( f ) ) && isspace( c ) )
      ;
    if ( c == EOF )
      break;

    //
    fseek( f, -1, SEEK_CUR );
    putc( toupper( c ), f );
    fseek( f, ftell( f ), SEEK_SET ); // add this line if you're using visual c

    //
    while ( ( c = getc( f ) ) != EOF && !isspace( c ) )
      ;
    if ( c == EOF )
      break;
  }

  fclose( f );

  return 0;
}

【讨论】:

  • 谢谢,但我想知道为什么这段代码不能正常工作?请帮帮我。
  • 谢谢,但是看看 -ftell- 函数。当我们不对单词进行任何活动而只是读取单词时,-ftell-return 的数字是不同的。我不明白为什么它不同。-ZDF
  • @user2583051 我不确定你在问什么。如果您从流中读取,流中的位置将被更新。如果当前位置为 0,而您使用 getc 读取一个字符,则新位置将为 1。如果您使用 fscanf,则位置将增加读取的字符数。
  • 请看我的评论。
  • @user2583051 你用的是什么编译器?
猜你喜欢
  • 1970-01-01
  • 2011-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-17
  • 1970-01-01
  • 2015-07-14
  • 1970-01-01
相关资源
最近更新 更多