试图操纵影响数组c ++的isspace函数

时间:2014-04-18 16:49:26

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

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

这是我的代码:

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

1 个答案:

答案 0 :(得分:1)

它无法正常工作的原因是您输入数据的string中没有空格。 istream默认情况下会在读取数据时跳过所有isspace(c) = true个字符(getline读取到&#34;行尾#34;字符除外)。在阅读任何数据之前,您should会先写infile >> noskipws来覆盖此类行为。