Well its simple: I'making a group of programs that use one file for settings, however not all settings are used for all programs so instead of making another variable is there any way to just throw, let's say the first line to null read the second line feed the third and fourth to null and read the fith? What's the difference between void and null?

Recommended Answers

All 4 Replies

is there any way to just throw, let's say the first line to null read the second line feed the third and fourth to null and read the fith?

Just read the line into a variable and don't do anything with it. When you read the next line, the variable will be overwritten. The following program uses that method to display only odd numbered lines:

#include <fstream>
#include <iostream>
#include <string>

int main()
{
    std::ifstream in("test.txt");
    std::string line;
    
    for (int i = 1; std::getline(in, line); i++) {
        if (i % 2 != 0)
            std::cout << i << ": '" << line << "'\n";
    }
}

ok that's what I'm doing just thought it would make my programs run faster (not much at all though) Now the second question: What's the difference between Void and Null?

What's the difference between Void and Null?

What's the context?

Null is defined as 0. Whenever you use null you could also use 0 although null is used more for pointers since the pointer value of 0 is reserved. Void is a data type with absolutely no space. It is used as a pointer to be a generic data type. EG:

char *mystr = NULL;
char *mystr2 = 0;//this is the same as above
void myvoid;//this is nothing, I am not sure if it will even be allocated
void *myvoidpntr;//this cannot be used directly but can be cast into any pointer type, it is generally quite dangerous to use this
void myfunction()//this function does not return anything, so it just returns void

Hope this helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.