找到你要的答案

Q:saving a char pointer on a file with c++

Q:一个C++文件保存一个字符指针

Hi I'm having trouble with a program which receives a text string provided by the user and does 3 things:

  1. Convert all vowels to capital letters, print the new text and also print the number of vowels converted.
  2. Save the new text into a file.
  3. Print the converted text character by character.

Heres the code

#include <stdio.h>
void convert(char *ptrQuote[80], int nvocals);
void save(char *ptrQuote[80]);
void print(char *ptrQuote[80]);

int main(int argc, char** argv) {

    char  quote[80];
    char *ptrQuote[80];
    int nvocals= 0;
    printf("Enter your quote :\n");
    scanf("%s", &quote);
    for(int i= 0; i<80; i++)
    {
        ptrQuote[i]= &quote[i];
    }
    convert(ptrQuote, nvocals);
    save(ptrQuote);
    print(ptrQuote);
    return 0;
}

void convert(char *ptrQuote[80], int nvocals)
{
    for(int i=0; i<80; i++)
    {
        if(*ptrQuote[i]=='a')
        {
            *ptrQuote[i]='A';
            nvocals ++;
        }

        if(*ptrQuote[i]=='e')
        {
            *ptrQuote[i]='E';
            nvocals ++;
        }

        if(*ptrQuote[i]=='i')
        {
            *ptrQuote[i]='I';
            nvocals ++;
        }

        if(*ptrQuote[i]=='o')
        {
            *ptrQuote[i]='O';
            nvocals ++;
        }

        if(*ptrQuote[i]=='u')
        {
            *ptrQuote[i]='U';
            nvocals ++;
        }

    }

    printf("%s\n", *ptrQuote);
    printf("Number of vocals: %d\n", nvocals);
}

void save(char *ptrQuote[80])
{
    FILE *quote;
    quote= fopen("quote.txt", "w");
    fwrite(&ptrQuote[80], sizeof(*ptrQuote), 1, quote);
    fclose(quote);
}

void print(char *ptrQuote[80])
{
    for(int i=0; i<sizeof(*ptrQuote); i++)
    {
        printf("%c", *ptrQuote[i]);
    }
}

The first function works properly but when it comes to the save and print functions it saves and prints the quote incomplete. As you can see I'm working with pointers. What can I do? Thanks.

嗨,我有麻烦的程序接收一个文本字符串提供的用户和做3件事:

  1. Convert all vowels to capital letters, print the new text and also print the number of vowels converted.
  2. Save the new text into a file.
  3. Print the converted text character by character.

这里的代码

#include <stdio.h>
void convert(char *ptrQuote[80], int nvocals);
void save(char *ptrQuote[80]);
void print(char *ptrQuote[80]);

int main(int argc, char** argv) {

    char  quote[80];
    char *ptrQuote[80];
    int nvocals= 0;
    printf("Enter your quote :\n");
    scanf("%s", &quote);
    for(int i= 0; i<80; i++)
    {
        ptrQuote[i]= &quote[i];
    }
    convert(ptrQuote, nvocals);
    save(ptrQuote);
    print(ptrQuote);
    return 0;
}

void convert(char *ptrQuote[80], int nvocals)
{
    for(int i=0; i<80; i++)
    {
        if(*ptrQuote[i]=='a')
        {
            *ptrQuote[i]='A';
            nvocals ++;
        }

        if(*ptrQuote[i]=='e')
        {
            *ptrQuote[i]='E';
            nvocals ++;
        }

        if(*ptrQuote[i]=='i')
        {
            *ptrQuote[i]='I';
            nvocals ++;
        }

        if(*ptrQuote[i]=='o')
        {
            *ptrQuote[i]='O';
            nvocals ++;
        }

        if(*ptrQuote[i]=='u')
        {
            *ptrQuote[i]='U';
            nvocals ++;
        }

    }

    printf("%s\n", *ptrQuote);
    printf("Number of vocals: %d\n", nvocals);
}

void save(char *ptrQuote[80])
{
    FILE *quote;
    quote= fopen("quote.txt", "w");
    fwrite(&ptrQuote[80], sizeof(*ptrQuote), 1, quote);
    fclose(quote);
}

void print(char *ptrQuote[80])
{
    for(int i=0; i<sizeof(*ptrQuote); i++)
    {
        printf("%c", *ptrQuote[i]);
    }
}

第一个函数正常工作,但当涉及到保存和打印功能,它保存和打印报价不完整。正如你所看到的,我正在使用指针。我能做什么?谢谢.

answer1: 回答1:

You were close. Instead of using 80 pointers (maybe a little overboard) for your code, just use one:

#include <stdio.h>
void convert(char ptrQuote[80], int nvocals);
void save(char ptrQuote[80]);
void print(char ptrQuote[80]);

int main(int argc, char **argv) {
    char  quote[80];
    int nvocals = 0;
    printf("Enter your quote :\n");
    scanf("%s", &quote);
    convert(quote, nvocals);
    save(quote);
    print(quote);
    return 0;
}

void convert(char ptrQuote[80], int nvocals)
{
    for(int i=0; i<80; i++)
    {
        if(ptrQuote[i]=='a')
        {
            ptrQuote[i]='A';
            nvocals ++;
        }

        if(ptrQuote[i]=='e')
        {
            ptrQuote[i]='E';
            nvocals ++;
        }

        if(ptrQuote[i]=='i')
        {
            ptrQuote[i]='I';
            nvocals ++;
        }

        if(ptrQuote[i]=='o')
        {
            ptrQuote[i]='O';
            nvocals ++;
        }

        if(ptrQuote[i]=='u')
        {
            ptrQuote[i]='U';
            nvocals ++;
        }

    }

    printf("%s\n", ptrQuote);
    printf("Number of vocals: %d\n", nvocals);
}

void save(char ptrQuote[80])
{
    FILE *quote;
    quote = fopen("quote.txt", "w");
    fwrite(ptrQuote, sizeof(*ptrQuote), 1, quote);
    fclose(quote);
}

void print(char ptrQuote[80])
{
    /* no need for loop here anymore */
    printf("%s\n", ptrQuote);
}

You also would be able to print to the file (using your method) with this:

for (int i = 0; i < 80; ++i)
    fputc(*ptrQuote[i], quote);

你很接近。而不是使用80个指针(也许有点落水)为您的代码,只使用一个:

#include <stdio.h>
void convert(char ptrQuote[80], int nvocals);
void save(char ptrQuote[80]);
void print(char ptrQuote[80]);

int main(int argc, char **argv) {
    char  quote[80];
    int nvocals = 0;
    printf("Enter your quote :\n");
    scanf("%s", &quote);
    convert(quote, nvocals);
    save(quote);
    print(quote);
    return 0;
}

void convert(char ptrQuote[80], int nvocals)
{
    for(int i=0; i<80; i++)
    {
        if(ptrQuote[i]=='a')
        {
            ptrQuote[i]='A';
            nvocals ++;
        }

        if(ptrQuote[i]=='e')
        {
            ptrQuote[i]='E';
            nvocals ++;
        }

        if(ptrQuote[i]=='i')
        {
            ptrQuote[i]='I';
            nvocals ++;
        }

        if(ptrQuote[i]=='o')
        {
            ptrQuote[i]='O';
            nvocals ++;
        }

        if(ptrQuote[i]=='u')
        {
            ptrQuote[i]='U';
            nvocals ++;
        }

    }

    printf("%s\n", ptrQuote);
    printf("Number of vocals: %d\n", nvocals);
}

void save(char ptrQuote[80])
{
    FILE *quote;
    quote = fopen("quote.txt", "w");
    fwrite(ptrQuote, sizeof(*ptrQuote), 1, quote);
    fclose(quote);
}

void print(char ptrQuote[80])
{
    /* no need for loop here anymore */
    printf("%s\n", ptrQuote);
}

您也将能够打印到文件(使用您的方法)与此:

for (int i = 0; i < 80; ++i)
    fputc(*ptrQuote[i], quote);
c++  file  pointers