Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
Hello guys. I want to ask if there is a way to generate a txt file that contains 1.000.000 different numbers all in one line.

Is there a way to do it? I use both linux and windows machines but I prefer to use a C program code.
Posted
Comments
Mehdi Gholam 20-Nov-14 8:17am    
What have you done so far?
[no name] 20-Nov-14 8:33am    
nothing I don't knwo how to program it. I just want to test my other programm and for that i need a txt file that will contain 1.000.000 numbers all in one line!
Tomas Takac 20-Nov-14 8:41am    
How big should the numbers be? How they should be separated?

If you want to use C, something like this might work for you;
C
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

char* append_char(char* buffer, char character) {
    *buffer = character;
    return ++buffer;
}

char* append_number(char* buffer, int number, char* temp_buffer) {
    char* target = buffer;
    char* source = temp_buffer;
    char* p;
    itoa(number, temp_buffer, 10);
    for (p = source; *p != 0; ++p) {
        *target = *p;
        ++target;
    }
    return target;
}

int main(int argc, char* argv[]) {
    // Max int is 4294967295, add one for a 
    // possible minus sign, add one for the space delimiter
    int number_max_characters = 10 + 1 + 1;
    int count_to_generate = 10; // This the number of numbers you want
    char* temp_buffer = (char*)malloc(number_max_characters);
    // Allocate space for all the numbers plus one for the "header",
    // plus space for the line-break after the header and a trailing zero
    // Note that buffer is potentially too big here, will fix that by copying 
    // only what we need into result, and freeing buffer
    char* buffer = (char*)malloc((count_to_generate + 1) * number_max_characters + 2 + 1);
    char* result;
    char* p = buffer;
    int i;
    FILE* fp;

    // Append header and line break
    p = append_char(append_char(append_number(p, count_to_generate, temp_buffer), '\r'), '\n');
    for (i = 0; i < count_to_generate; ++i) {
        p = append_number(p, i, temp_buffer);
        if (i < count_to_generate - 1)
            p = append_char(p, ' ');
    }

    *p = 0; // Null terminate

    // Take the used size of buffer, allocate that to result...
    result = (char*)malloc((int)p - (int)buffer);
    // ...and copy over to result
    strcpy(result, buffer);
    free(buffer); // We're done with this one now

    printf("%s", result);

    fp = fopen("c:\\temp\\test.txt", "w");
    fprintf(fp, "%s", result);
    fclose(fp);

    return 0;
}

Hope this helps,
Fredrik
 
Share this answer
 
v2
Slightly simpler than Fredrik's solution:
C++
#define MAXIMUM 10//00000

int main
{
    FILE* fp;
    int count, number;
    char szNumber[16];
    
    fp = fopen("testfile.txt", "w");
    for (count = 1; count <= MAXIMUM; ++count)
    {
        number = 10 + count;
        sprintf(szNumber, "%d", number);
        fputs(szNumber, fp);
        if (count < MAXIMUM)
            fputs(",", fp);
    }
    fclose(fp);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900