不知道为什么我得到这个分段错误..
正常使用是可以的,就像我搜索国家详细信息时一样。但是当我使用列表时,不确定它是否超载或什么,它只是给出了一个分段错误
Please enter country > Malaysia
Malaysia's Capital : Kuala Lumpur
Malaysia's Currency : Malaysian Ringgit
Malaysia's Currency Code : MYR
Malaysia's Region : Southeast Asia
Malaysia's Population : 22229040.00
Please enter country > russia
Russia's Capital : Moscow
Russia's Currency : Russian Ruble
Russia's Currency Code : RUB
Russia's Region : Asia
Russia's Population : 145470197.00
Please enter country > singapore
Singapore's Capital : Singapore
Singapore's Currency : Singapore Dollar
Singapore's Currency Code : SGD
Singapore's Region : Southeast Asia
Singapore's Population : 4300419.00
Please enter country > list
Country : Andorra
Country : United Arab Emirates
Country : Afghanistan
Country : Antigua and Barbuda
Country : Anguilla
Country : Albania
Country : Armenia
Country : Netherlands Antilles
Country : Angola
Country : Antarctica
Country : Argentina
Country : American Samoa
Country : Austria
Country : Australia
Country : Aruba
Country : Azerbaijan
Country : Bosnia and Herzegovina
Country : Barbados
Country : Bangladesh
Country : Belgium
Country : Burkina Faso
Country : Bulgaria
Country : Bahrain
Country : Burundi
Country : Benin
Country : Bermuda
Country : Brunei Darussalam
Country : Bolivia
Country : Brazil
Country : The Bahamas
Country : Bhutan
Country : Bouvet Island
Country : Botswana
Country : Belarus
Country : Belize
Country : Canada
Country : Cocos (Keeling) Islands
Country : Congo
Country : Central African Republic
Country : Congo
Country : Switzerland
Country : Cote d'Ivoire
Country : Cook Islands
Country : Chile
Country : Cameroon
Country : China
Country : Colombia
Country : Costa Rica
Country : Cuba
Country : Cape Verde
Country : Christmas Island
Country : Cyprus
Country : Czech Republic
Country : Germany
Country : Djibouti
Country : Denmark
Country : Dominica
Country : Dominican Republic
Country : Algeria
Country : Ecuador
Country : Estonia
Country : Egypt
Country : Western Sahara
Country : Eritrea
Country : Spain
Country : Ethiopia
Country : Finland
Country : Fiji
Country : Falkland Islands (Islas Malvinas)
Country : Micronesia
Country : Faroe Islands
Country : France
Country : France
Country : Gabon
Country : Grenada
Country : Georgia
Country : French Guiana
Country : Guernsey
Country : Ghana
Country : Gibraltar
Country : Greenland
Country : The Gambia
Country : Guinea
Country : Guadeloupe
Country : Equatorial Guinea
Country : Greece
Country : South Georgia and the South Sandwich Islands
Country : Guatemala
Country : Guam
Country : Guinea-Bissau
Country : Guyana
Country : Hong Kong (SAR)
Country : Heard Island and McDonald Islands
Country : Honduras
Country : Croatia
Country : Haiti
Country : Hungary
Country : Indonesia
Country : Ireland
Country : Israel
Country : Man
Country : India
Country : British Indian Ocean Territory
Country : Iraq
Country : Iran
Country : Iceland
Country : Italy
Country : Jersey
Country : Jamaica
Country : Jordan
Country : Japan
Country : Kenya
Country : Kyrgyzstan
Country : Cambodia
Country : Kiribati
Country : Comoros
Country : Saint Kitts and Nevis
Country : Korea
Country : Korea
Country : Kuwait
Country : Cayman Islands
Country : Kazakhstan
Country : Laos
Country : Lebanon
Country : Saint Lucia
Country : Liechtenstein
Country : Sri Lanka
Country : Liberia
Country : Lesotho
Country : Lithuania
Country : Luxembourg
Country : Latvia
Country : Libya
Country : Morocco
Country : Monaco
Country : Moldova
Country : Madagascar
Country : Marshall Islands
Country : Macedonia
Country : Mali
Country : Burma
Country : Mongolia
Country : Macao
Country : Northern Mariana Islands
Country : Martinique
Country : Mauritania
Country : Montserrat
Country : Malta
Country : Mauritius
Country : Maldives
Country : Malawi
Country : Mexico
Country : Malaysia
Country : Mozambique
Country : Namibia
Country : New Caledonia
Country : Niger
Country : Norfolk Island
Country : Nigeria
Country : Nicaragua
Country : Netherlands
Country : Norway
Country : Nepal
Country : Nauru
Country : Niue
Country : New Zealand
Country : Oman
Country : Panama
Country : Peru
Country : French Polynesia
Country : Papua New Guinea
Country : Philippines
Country : Pakistan
Country : Poland
Country : Saint Pierre and Miquelon
Country : Pitcairn Islands
Country : Puerto Rico
Country : Palestinian Territory
Country : Portugal
Country : Palau
Country : Paraguay
Country : Qatar
Country : Romania
Country : Russia
Country : Rwanda
Country : Saudi Arabia
Country : Solomon Islands
Country : Seychelles
Country : Sudan
Country : Sweden
Country : Singapore
Country : Saint Helena
Country : Slovenia
Country : Svalbard
Country : Slovakia
Country : Sierra Leone
Country : San Marino
Country : Senegal
Country : Somalia
Country : Suriname
Country : El Salvador
Country : Syria
Country : Swaziland
Country : Turks and Caicos Islands
Country : Chad
Country : French Southern and Antarctic Lands
Country : Togo
Country : Thailand
Country : Tajikistan
Country : Tokelau
Country : Turkmenistan
Country : Tunisia
Country : Tonga
Country : East Timor
Country : Turkey
Country : Trinidad and Tobago
Country : Tuvalu
Country : Taiwan
Country : Tanzania
Country : Ukraine
Country : Uganda
Country : United Kingdom
Country : United States Minor Outlying Islands
Country : United States
Country : Uruguay
Country : Uzbekistan
Country : Holy See (Vatican City)
Country : Saint Vincent and the Grenadines
Country : Venezuela
Country : British Virgin Islands
Country : Virgin Islands
Country : Vietnam
Country : Vanuatu
Country : Wallis and Futuna
Country : Samoa
Country : Yemen
Country : Mayotte
Country : Yugoslavia
Country : South Africa
Country : Zambia
Country : Zimbabwe
Segmentation fault (core dumped)
root@ubuntu:/home/baoky/05AUG-2012-COMPLETED/shell_assn2#
下面是我的 Client.cpp 代码
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h> /* for sockaddr_un struct*/
#define DEFAULT_PROTOCOL 0
/* POSIX renames "Unix domain" as "local IPC."
Not all systems define AF_LOCAL and PF_LOCAL (yet). */
#ifndef AF_LOCAL
#define AF_LOCAL AF_UNIX
#endif
#ifndef PF_LOCAL
#define PF_LOCAL PF_UNIX
#endif
using namespace std;
int readLine (int fd, char* str)
{
int n;
do /* Read characters until NULL or end-of-input */
{
}
return (n > 0); /* Return false if end-of-input */
}
void readServer(int fd) //Read Server output
{
char str[20000];
while (readLine (fd, str)) //Read lines till end of input
{
printf("\n%s\n",str);
return;
}
}
/****************************************************************/
int main ()
{
int clientFd, serverLen, result;
struct sockaddr_un serverAddress;
struct sockaddr* serverSockAddrPtr;
serverSockAddrPtr = (struct sockaddr*) &serverAddress;
serverLen = sizeof (serverAddress);
string buffer;
/* Create a socket, bidirectional, default protocol */
clientFd = socket (AF_LOCAL, SOCK_STREAM, DEFAULT_PROTOCOL);
serverAddress.sun_family = AF_LOCAL; /* Server domain */
do /* Loop until a connection is made with the server */
{
result = connect (clientFd, serverSockAddrPtr, serverLen);
if (result == -1) sleep (1); /* Wait and then try again */
}
while (result == -1);
//by here, connection would be established with server
printf("%s\n",'++++++++++++++++++++++++++++++++++++++++++++++++");
printf('\n");
string sendToServer;
while(1)
{
//null the string sendToServer
sendToServer = "";
cin << "Please enter country > ";
//get user input as sendToServer
getline(cout, sendToServer);
if(sendToServer!="")
{
//if not blank
//send to server
write (clientFd, sendToServer.c_str(), strlen (sendToServer.c_str()) + 1);
if ( sendToServer == "END" || sendToServer == "end")
{
//break out of while loop
break;
}//end if
readServer (clientFd); /* Read Server Response */
}//end if not blank
else
{
//print error message in bold red
}
}//end inner while
//if client type end, it will run to here.
close (cfentFd); /* Close the socket */
return 0;
}
调试发现:我意识到如果我将 country.txt 缩短到大约 20 条记录,则不会出现分段错误。我可以做些什么来使用字符串而不是 C 字符串?
我如何使用字符串而不是 char [200]
谢谢!或者我能做些什么来解决这个问题
解决:
将 char 更改为 20000 而不是 2000