我正在阅读文本文件中的行,我想知道这是否是一个好方法?我必须编写函数numberoflines来减少number_of_lines variable一个,因为在while循环中,对于它读取的每一行,它都会向number_of_lines变量添加2.
#include <iostream>
#include <fstream>
using namespace std;
int number_of_lines = 0;
void numberoflines();
int main(){
string line;
ifstream myfile("textexample.txt");
if(myfile.is_open()){
while(!myfile.eof()){
getline(myfile,line);
cout<< line << endl;
number_of_lines++;
}
myfile.close();
}
numberoflines();
}
void numberoflines(){
number_of_lines--;
cout<<"number of lines in text file: " << number_of_lines << endl;
}
Run Code Online (Sandbox Code Playgroud)
还有其他更容易更好的方法吗?
我一直想知道这一点,但仍未找到答案.每当我们使用"cout"或"printf"时,屏幕上印刷的是什么?文本是如何形成的......(这里可能是一个非常模糊的问题,无论你给我什么,我都会做错.).那么基本上这些功能是如何制作的?它是汇编吗?如果是的话,它从哪里开始?这带来了更多问题,比如他们如何制作openGl/directx函数.
分解人们将其分解.:)
我刚刚通过"Beej的网络编程指南"一书开始学习winsock.我正在windows下编程并通过gcc运行它.这只是编写我的第一个服务器程序的开始,但是当我尝试编译时它给了我这些错误.
/* Server */
#include <iostream>
#include <windows.h>
#include <winsock2.h>
using namespace std;
const int winsockVersion = 2;
#define BACKLOG 10
#define PORT 3000
int main(void){
WSADATA wsadata;
if (WSAStartup(MAKEWORD(winsockVersion,0),&wsadata) == 0){
struct addrinfo hints, *res;
memset(&hints,0,sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
if ( getaddrinfo(NULL,PORT,&hints,&res) == 0 ){
cout<<"-Call to get addrinfo successful!." << endl;
}
cout<<"res af_family" << res->ai_family << endl;
}
//clear stuff
if( WSACleanup() != 0){
cout<<"-WSACleanup unsuccessful" << endl;
}else{
cout<<"-WSACleanup …Run Code Online (Sandbox Code Playgroud) 我是新的GUI和编程整体,到目前为止我对c有一个大致的了解并且花了很多时间编写控制台应用程序.
我正在尝试学习GUI但到目前为止还没有成功.我试过学习wxwidgets(通过官方文档),gtk(通过官方文档)和win32(forger win32教程),但仍然没有完全到达那里.
我仍然真的想但似乎找不到任何好的材料.你会推荐什么作为初学者的开始?
我一直在关注forgers win32教程,特别是此时此部分,并且想知道在编译win32程序时如何链接.rc(资源)文件?(我正在通过命令行编译).
我正在阅读这篇文章说你可以做这样的事情windres chocolate-doom-res.rc chocolate-doom-res.o并以这种方式编译gcc other.o files.o etc.o chocolate-doom-res.o -o chocolate-doom.exe
但是当我尝试做windres res.rc res.o(res.rc是我的资源文件)时,它给了我这个windres: res.rc:3: syntax error
res.rc
#include "resource.h"
IDR_MYMENU
BEGIN
POPUP "&File"
BEGIN
MENUITEM "E&xit", ID_FILE_EXIT
END
POPUP "&Stuff"
BEGIN
MENUITEM "&Go", ID_STUFF_GO
MENUITEM "G&o somewhere else",0,GRAYED
END
END
IDI_MYICON ICON "menu_one.ico"
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?.
我一直在关注Beej Networking指南,在服务器部分,有一部分代码叫做函数fork().
if (!fork()) { // this is the child process
close(sockfd); // child doesn't need the listener
if (send(new_fd, "Hello, world!", 13, 0) == -1)
perror("send");
close(new_fd);
exit(0);
Run Code Online (Sandbox Code Playgroud)
我在Windows机器上,不能让那部分工作.我能做些什么来解决这个问题?我的代码如下.
/* Server */
#define _WIN32_WINNT 0x501
#include <iostream>
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <sys/types.h>
using namespace std;
const int winsockVersion = 2;
#define BACKLOG 10
#define PORT "3000"
int main(void){
WSADATA wsadata;
if (WSAStartup(MAKEWORD(winsockVersion,0),&wsadata) == 0){
cout<<"-WSAStartup initialized..." << endl;
int status;
int sockfd, …Run Code Online (Sandbox Code Playgroud) 我已经给了私有的int变量x和y的类,以及一个运算符重载函数,
class Bag{
private:
int x;
int y;
public:
Bag();
~Bag();
//.......
//.....etc
};
Bag operator+ (Bag new) const{
Bag result(*this); //what does this mean?
result.x += new.x;
result.y += new.y;
}
Run Code Online (Sandbox Code Playgroud)
有"袋子结果(*this);"的效果是什么?那里?.
我在使用此警告消息时遇到了一些问题,它是在模板容器类中实现的
int k = 0, l = 0;
for ( k =(index+1), l=0; k < sizeC, l < (sizeC-index); k++,l++){
elements[k] = arryCpy[l];
}
delete[] arryCpy;
Run Code Online (Sandbox Code Playgroud)
这是我得到的警告
cont.h: In member function `void Container<T>::insert(T, int)':
cont.h:99: warning: left-hand operand of comma has no effect
cont.h: In member function `void Container<T>::insert(T, int) [with T = double]':
a5testing.cpp:21: instantiated from here
cont.h:99: warning: left-hand operand of comma has no effect
cont.h: In member function `void Container<T>::insert(T, int) [with T = std::string]':
a5testing.cpp:28: instantiated …Run Code Online (Sandbox Code Playgroud) 我在c:/ programfiles中安装了gcc(也设置为路径变量),我从http://www.gtk.org/download-windows.html,glib,gtk,pango获得gtk的所有必要文件, atk和cairo.虽然我不知道如何使用gtk和gcc编译器编译ac程序.如何设置所有内容以使其有效?(我不知道每个zip文件的位置.?)基本上我真的不知道从哪里开始.
嗨,我有一个图像,上面画着黑色矩形,背景是透明的.该文件保存为png(clear.png).然后我有另一个图像,它只是一个固定的红色背景,保存为jpeg(background.jpeg).我试图做的是使clear.png中的黑色矩形显示在纯红色背景图像的顶部.
这就是我所做的......
/*Transparent image*/
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <iostream>
using namespace std;
int main(int argc,char *argv[]){
SDL_Surface *screen = NULL;
SDL_Surface *background = NULL;
SDL_Surface *transparentimage = NULL;
if ( SDL_Init(SDL_INIT_EVERYTHING) == -1){
cout <<"could not start sdl" << endl;
}
screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
if ( screen == NULL){
cout<<"could not create the screen" << endl;
}
background = IMG_Load("background.jpeg");
if ( background == NULL){
cout<<"could not load background" << endl;
}
transparentimage = IMG_Load("clear.png"); …Run Code Online (Sandbox Code Playgroud)