2015-09-07 79 views
-3

我遇到了下面的代码问题,因为我似乎无法得到strncpy只输出命令行参数的前3个字符我已经通过我的程序。另外,我无法将修剪过的字符串打印到 ostream,我将它们传递给我的重载操作符。 主和所有模块的代码粘贴下面:在使用C++输出字符串和strncpy时遇到问题

#include "Cstring.h" 
#include <cstring> 
#include <iostream> 
using namespace w1; 
int w1::characters = 3; 
Cstring::Cstring(char* orginal) { 
    if (orginal == nullptr) 
     trimmed = nullptr; 

     //strcpy(orginal,trimmed); 
     cout << orginal << trimmed; 
     strcpy(orginal,trimmed); 
} 
ostream& Cstring::display(ostream& output) { 
     output << trimmed; 
     return output; 
} 
ostream& w1::operator<<(ostream& console,Cstring& input) { 
    static int arguments = 0; 
    arguments++; 
    return console << arguments << input.display(console) << "\n"; 
} 


#ifndef CSTRING_H 
#define CSTRING_H 
#include <ostream> 
using namespace std; 
namespace w1 { 
    extern int characters; 
    class Cstring { 
      const int max = 3; 
      char *trimmed; 
      public: 
       Cstring(char* orignal); 
       ostream& display(ostream& output); 
    }; 
    ostream& operator<<(ostream& console,Cstring& input); 

} 
#endif 


#include "Cstring.h" 
using namespace std; 
using namespace w1; 
void process(char* user_data); 



    #include "Cstring.h" 
    #include <iostream> 
    using namespace std; 
    using namespace w1; 
    void process(char* user_data) { 
     Cstring trimmed(user_data); 
     cout << trimmed 
    } 

    #include "process.h" 
    #include "Cstring.h" 
    #include <iostream> 
    #include <cstring> 
    using namespace std; 
    using namespace w1; 
    int main(int argc, char* argv[]) { 
     if (argc == 1) 
      cout << argv[0] << " Insuffienct number of arguments(min 1)" << endl; 
     cout << "Maximum number of characters stored " << w1::characters << endl; 
      for (int x = 1 ; x < argc; x++) { 
       process(argv[x]); 


     } 
} 
+0

可能值得看格式。此外,你似乎已经重复了一些代码。 –

+1

在'strcpy'目标是第一个参数。另外,我没有看到为“裁剪”分配空间的位置。 (例如:'trimmed = new char [(strlen(original)+ 1]') –

+0

您没有在代码中使用'strncpy'。您使用过'strcpy'。问题或代码中是否有错字? – user2079303

回答

0

你PROGRAMM是有点乱。所有你似乎想要做的是输出的每个命令行参数的前3个字符,你想知道如何使用strncpy()函数,所以这里是一个最小的例子来说明这一点:

#include <cstring> 
#include <iostream> 
using namespace std; 

int main(int argc, char *argv[]) 
{ 
    for (int i = 1; i < argc; ++i) 
    { 
     char buffer[4] = { 0 }; 
     strncpy(buffer, argv[i], 3); 
     cout << buffer << endl; 
    } 
} 

该代码使用strncpy来将每个命令行参数的前3个字符复制到一个缓冲区中(大小为4,因为还需要有一个空终止符),然后打印缓冲区的内容。只有3个第一个字符被复制。你必须自己照顾无效终结者。

+0

trimmed = new char [ (strlen的(原单))+ 1]; \t \t strncpy()函数(修剪,原单,3); –

+0

这是我在两行加现在我得到:oop10x6020c8 btp20x6020c8 –

+0

下面是分配的链接解释什么,我需要要做,https://scs.senecac.on.ca/~oop345/pages/workshops/w1.html –

0

1)你从来没有初始化,当你把它分配给nullptr如果originalnullptr除了Cstring::trimmed。当您尝试从nullptr或单元化的trimmed复制它strcpy(orginal,trimmed);行时,您会收到未定义的行为。

2)你可能不打算从Cstring::trimmed复制到original这是argv[x],而是意味着从复制到original代替Cstring::trimmed。但是在那种情况下,你仍然有未定义的行为,因为你没有分配任何内存或初始化Cstring::trimmed(见1)。 3)您将w1::charactersCstring::max定义为3,但您从不使用其中任何一种(除非在main中输出w1::characters)。我会假设你打算使用其中的一个来限制输出为3个字符。

+0

修复基于您的意见,以及我忘了分配字符串时感到愚蠢。然而,运营商<<不工作,但内部显示器工作 –

相关问题