繁体   English   中英

C++ 我需要了解在哪里使用指针和双指针

[英]C++ I need to understand where to use pointers and double pointers

我有这个基本的多功能工具程序,它的目标是在一个包含一些字符串的文件上完成四个函数.. 取一个字符串,例如,取一行并将它的大写。 我知道它还不完美,但我需要了解为什么我的 char* 和 char** 会搞得一团糟。

非常感谢您的时间


#include <iostream> //to use enter and exit
#include <cstring>
#include <string> 
#include <fstream> //to use files
using namespace std;

/*--------------------------FONCTION PART-----------------------------------*/

//define one fonction for each transformation 

//FONCTION 1 

void fonctionu (char* argv){
   
   char pattern = argv[2];
   char file = argv[3];
   
   fstream data(file, ios::in);  
   
   if (data){
     string line;

     while (getline (data ,line)){
      unsigned found = line.find(pattern);
      if (found != string::npos) {
       for (int i = 0; line[i]!='\0'; i++) {
         if(line[i] >= 'a' && line[i] <= 'z') {
           line[i] = line[i] - (32);// (ASCII)
           cout<<line[i];
      }}  }
      if (found == string::npos) {
        for (int i = 0; line[i]!='\0'; i++) {
         cout<<line[i];
        }}}}}

//FONCTION 2

void fonctiond( char* argv){ //remove line with xyz

  char pattern = argv[2];
  char file = argv[3];
  fstream data(file, ios::in);
  
  if (data){
     string line;
     while (getline (data ,line)){
      unsigned found = line.find(pattern); //as a reminder argv[2] is the pattern (warning : it need to be an unsigned to compare two unsigned)
      if (found != string::npos) {
      //delete the line of the file when there is the pattern 
       cout<<'\0'; }
      if (found == string::npos){
        for (int i = 0; line[i]!='\0'; i++) {
          cout<< line[i];
     }}}}}

//FONCTION 3 

void fonctionc( char* argv){

  char pattern = argv[2];
  char file = argv[3];
  fstream data(file, ios::in); 
  
  if (data ){
     string line;
     while (getline (data ,line)){
       unsigned found = line.find(pattern); //as a reminder argv[2] is the pattern 
       if (found != string::npos) {
        for (int i = 0; line[i]!='\0'; i++) {
          cout<< "\033[31m"<< line[i]; //the line will be red
        } 
       }
       if (found == string::npos){
        for (int i = 0; line[i]!='\0'; i++) {
          cout<< line[i];
       }}}}}

//FONCTION 4 
//replace the pattern xyz by abc
void fonctions ( char* argv){

  char pattern = argv[2];
  char file = argv[3];
  fstream data(file, ios::in); 
  
  if (data ){
     string line;

     while (getline (data ,line)){
      unsigned found = line.find(pattern);
      if (found != string::npos) {
       for (int i = 0; line[i]!='\0'; i++) {
         if(line[i] >= 'a' && line[i] <= 'z') {
           line[i] = line[i] - (97); // (ASCII) //we creat a shift that allow to make the x became a 'a' and the y a 'b'...etc
           cout<<line[i];}} 
      } 
      if (found == string::npos) {
       for (int i = 0; line[i]!='\0'; i++) {
          cout<<line[i];
      }}}}}


/*--------------------------MAIN PART---------------------------------------*/

int main(char* argv){

  // ipsacs : argv[0]
  //option : 
  string a = argv[1]; //string ! to compare line 103,108 and 112
  // pattern : argv[2];
  //file :  argv[3];
  
  if (argv[1]=='\0' || argv[2]=='\0'){
    cout <<"ERROR"<<'\n';}

  if (a == "u"){
     char fonctionu (argv);}
  if (a== "d"){
     char fonctiond(argv);}
  if (a == "c"){
       char fonctionc(argv);}
  if (a == "s"){
       char fonctions(argv);} 
  return 0;
  }

/*--------------------------TEST PART--------------------------------------*/
/*
pour compiler : g++ -Wall ipsacs.cpp -o ipsacs
./ipsacs u xyz foo //how the program would receive arguments, basically

Le programme ne renvoie rien. 
*/

这是编译日志:

main.cpp:22:30: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
    fstream data(file, ios::in);  
                              ^
In file included from main.cpp:8:0:
/usr/include/c++/6/fstream:902:7: note:   initializing argument 1 of ‘std::basic_fstream<_CharT, _Traits>::basic_fstream(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits; std::ios_base::openmode = std::_Ios_Openmode]’
       basic_fstream(const char* __s,
       ^~~~~~~~~~~~~
main.cpp: In function ‘void fonctiond(char*)’:
main.cpp:46:29: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
   fstream data(file, ios::in);
                             ^
In file included from main.cpp:8:0:
/usr/include/c++/6/fstream:902:7: note:   initializing argument 1 of ‘std::basic_fstream<_CharT, _Traits>::basic_fstream(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits; std::ios_base::openmode = std::_Ios_Openmode]’
       basic_fstream(const char* __s,
       ^~~~~~~~~~~~~
main.cpp: In function ‘void fonctionc(char*)’:
main.cpp:66:29: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
   fstream data(file, ios::in); 
                             ^
In file included from main.cpp:8:0:
/usr/include/c++/6/fstream:902:7: note:   initializing argument 1 of ‘std::basic_fstream<_CharT, _Traits>::basic_fstream(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits; std::ios_base::openmode = std::_Ios_Openmode]’
       basic_fstream(const char* __s,
       ^~~~~~~~~~~~~
main.cpp: In function ‘void fonctions(char*)’:
main.cpp:88:29: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
   fstream data(file, ios::in); 
                             ^
In file included from main.cpp:8:0:
/usr/include/c++/6/fstream:902:7: note:   initializing argument 1 of ‘std::basic_fstream<_CharT, _Traits>::basic_fstream(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits; std::ios_base::openmode = std::_Ios_Openmode]’
       basic_fstream(const char* __s,
       ^~~~~~~~~~~~~
main.cpp: At global scope:
main.cpp:109:5: warning: first argument of ‘int main(char*)’ should be ‘int’ [-Wmain]
 int main(char* argv){
     ^~~~
main.cpp:109:5: warning: ‘int main(char*)’ takes only zero or two arguments [-Wmain]
main.cpp: In function ‘int main(char*)’:
main.cpp:113:20: error: conversion from ‘char’ to non-scalar type ‘std::string {aka std::basic_string}’ requested
   string a = argv[1]; //string ! to compare line 103,108 and 112
              ~~~~~~^
main.cpp:121:26: error: invalid conversion from ‘char*’ to ‘char’ [-fpermissive]
      char fonctionu (argv);}
                          ^
main.cpp:123:25: error: invalid conversion from ‘char*’ to ‘char’ [-fpermissive]
      char fonctiond(argv);}
                         ^
main.cpp:125:27: error: invalid conversion from ‘char*’ to ‘char’ [-fpermissive]
        char fonctionc(argv);}
                           ^
main.cpp:127:27: error: invalid conversion from ‘char*’ to ‘char’ [-fpermissive]
        char fonctions(argv);} 
                           ^

char *是指向包含字符的 memory 地址的指针,通常用于 c 字符串。

char **是指向 memory 地址的指针,该指针包含指向 memory 地址的指针,该地址包含一个字符,通常用于 c 字符串的 arrays。

如果不是有效形式,您的main签名,因为没有main形式只接受字符指针。 您最有可能寻找的形式是int main( int argc, int **argv ) 这需要两个参数: argc是传递给程序的 arguments 的数量,而argv包含作为 c 字符串传递给程序的参数。

在 main 方法中,您通常会确保argc是正确的,即您将正确数量的参数传递到 function (注意: argv[0]始终至少为 1 将是程序名称)。

std::string a = argv[1]很好,它会导致argv指向的字符串被转换为字符串。 调用函数时,您有char functionu(argv); . 您将要从该行中删除char 实际上,按照它的编写方式,您正在尝试创建一个名为functionuchar变量,其初始值为argv

你的每个函数,而不是接受一个char *应该接受一个char ** ,然后当你有类似char file = argv[3]; , 你会想把它改成char *file = argv[3]; , 或std::string file = argv[3];

暂无
暂无

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

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