【问题标题】:Trying to manipulating isspace function that effects array c++试图操纵影响数组 c++ 的 isspace 函数
【发布时间】:2014-04-18 16:49:25
【问题描述】:

我需要帮助获取 fillSpace 函数来将空格更改为我想要的任何字符。每次我调用函数时,它都不会更改在文件中找到的空格并在输出文件中输出新字符。

这是我的代码:

#include <iostream>
#include <fstream>
#include <cctype>
#include <cstdlib>
using namespace std;

struct Magazine
{
 string lastName;
 string firstName;
 string address;
 string city;
 string state;
 string country;
 string zip;
 string expDate;
 string subNum;
};

int readFile (ifstream &input, Magazine magList[], int size);
void writeFile (ofstream &output, Magazine magList[], int numBooks);
void fillSpace (string &expDate);

int main()
{
 Magazine magSub[260];
 ifstream  infile;
 ofstream outfile;

 int numBooks = 0;
 int i=0;
 char dummy;

 outfile.open("EXPIRED.txt");
 infile.open("INPUT.txt");
 if (infile.fail())
 {
    cout << "The file doesn't exist";
    exit(-1);
 }
 else
 {
    numBooks = readFile (infile, magSub, 260);

    for (i=0; i<numBooks; i++)
    {
        cout << "Last Name: " << magSub[i].lastName << endl;
        cout << "First Name: " << magSub[i].firstName << endl;
        cout << "Street Address: " << magSub[i].address << endl;
        cout << "City: " << magSub[i].city << endl;
        cout << "State or Province: " << magSub[i].state << endl;
        cout << "Country: " << magSub[i].country << endl << endl;
        cout << "Zip or Postal Code: " << magSub[i].zip << endl;
        cout << "Expiration Date: " << magSub[i].expDate << endl;
        cout << "Subscriber Number: " << magSub[i].subNum << endl << endl;

    fillSpace(magSub[i].expDate);

    writeFile(outfile, magSub, numBooks);

 }
}

void fillSpace (string &expDate)
{
 for (int i=0; i < expDate.length(); i++)
 {
     if (isspace(expDate[i]))
        expDate[i] = '0';
 }
}

int readFile (ifstream &input, Magazine magList[], int size)
{
 int i=0;
 char dummy;
 while (!input.eof())
 {
    getline (input, magList[i].lastName);
    getline (input, magList[i].firstName);
    getline (input, magList[i].address);
    getline (input, magList[i].city);
    getline (input, magList[i].state);
    getline (input, magList[i].country);
    input >> magList[i].zip;
    input.get(dummy);
    input >> magList[i].expDate;
    input.get(dummy);
    input >> magList[i].subNum;
    input.get(dummy);
    i++;
 }
 return i;
}
void writeFile (ofstream &output, Magazine magList[], int numBooks)
{
 for (int i=0; i<numBooks; i++)
 {
    output << "Last Name: " << magList[i].lastName << endl;
    output << "First Name: " << magList[i].firstName << endl;
    output << "Street Address: " << magList[i].address << endl;
    output << "City: " << magList[i].city << endl;
    output << "State or Province: " << magList[i].state << endl;
    output << "Country: " << magList[i].country << endl;
    output << "Zip or Postal Code: " << magList[i].zip << endl;
    output << "Expiration Date: " << magList[i].expDate << endl;
    output << "Subscriber Number: " << magList[i].subNum << endl << endl;
 }
}

【问题讨论】:

  • 这应该如何编译? fillSpace 接受 std::string&amp; 作为参数,但您传递的是整数 Magazine::expDate
  • 对不起,那些应该是字符串。

标签: c++ arrays function file-io struct


【解决方案1】:

它不起作用的原因是您输入数据的strings 中没有空格。 istreams 在读取数据时默认跳过所有 isspace(c) = true 字符(getline 读取到“行尾”字符除外)。你 should 在读取任何数据之前写 infile &gt;&gt; noskipws 以覆盖此类行为。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-28
    • 2012-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多