小编sil*_*ent的帖子

计算文本文件中的行数

我正在阅读文本文件中的行,我想知道这是否是一个好方法?我必须编写函数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)

还有其他更容易更好的方法吗?

c++ text gcc file

24
推荐指数
2
解决办法
17万
查看次数

深入了解事物如何印刷到屏幕上(cout,printf)以及我在教科书上似乎找不到的非常复杂的东西的起源

我一直想知道这一点,但仍未找到答案.每当我们使用"cout"或"printf"时,屏幕上印刷的是什么?文本是如何形成的......(这里可能是一个非常模糊的问题,无论你给我什么,我都会做错.).那么基本上这些功能是如何制作的?它是汇编吗?如果是的话,它从哪里开始?这带来了更多问题,比如他们如何制作openGl/directx函数.

分解人们将其分解.:)

c++ assembly cout libraries

23
推荐指数
2
解决办法
1060
查看次数

winsock编译错误,它无法找到addrinfo结构和一些相关的函数

我刚刚通过"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)

c++ gcc winsock

16
推荐指数
1
解决办法
1万
查看次数

用c编写GUI编程简介

我是新的GUI和编程整体,到目前为止我对c有一个大致的了解并且花了很多时间编写控制台应用程序.

我正在尝试学习GUI但到目前为止还没有成功.我试过学习wxwidgets(通过官方文档),gtk(通过官方文档)和win32(forger win32教程),但仍然没有完全到达那里.

我仍然真的想但似乎找不到任何好的材料.你会推荐什么作为初学者的开始?

c user-interface

15
推荐指数
2
解决办法
2万
查看次数

如何通过命令行使用gcc编译win32应用程序时链接.rc(资源)文件?

我一直在关注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)

有任何想法吗?.

c c++ resources winapi gcc

14
推荐指数
2
解决办法
6588
查看次数

在窗户中替代分叉

我一直在关注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)

c++ winapi gcc fork winsock

13
推荐指数
3
解决办法
2万
查看次数

关于c ++中"this"指针的问题

我已经给了私有的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);"的效果是什么?那里?.

c++ class operator-overloading this

8
推荐指数
2
解决办法
768
查看次数

逗号的左手操作数没有效果?

我在使用此警告消息时遇到了一些问题,它是在模板容器类中实现的

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++ gcc templates warnings

7
推荐指数
1
解决办法
1万
查看次数

在Windows下使用gcc安装gtk并进行编译?

我在c:/ programfiles中安装了gcc(也设置为路径变量),我从http://www.gtk.org/download-windows.html,glib,gtk,pango获得gtk的所有必要文件, atk和cairo.虽然我不知道如何使用gtk和gcc编译器编译ac程序.如何设置所有内容以使其有效?(我不知道每个zip文件的位置.?)基本上我真的不知道从哪里开始.

c gtk user-interface gcc install

6
推荐指数
1
解决办法
1万
查看次数

将透明的.PNG图像模糊到屏幕上

嗨,我有一个图像,上面画着黑色矩形,背景是透明的.该文件保存为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)

c++ transparency gcc sdl background-image

5
推荐指数
1
解决办法
6971
查看次数