Thread: null terminated strings

  1. #1
    Registered User
    Join Date
    Jan 2024
    Posts
    6

    null terminated strings

    I'm very new to C.. so please be patient

    I'm trying to read strings that were written to a file by a program written in Visual Basic... which means the strings are not null terminated.

    so what I'm doing is reading the entire file as a single array as 'char ttl[512]' and then using individual loops to copy out the data as needed like this:

    Code:
    for (int i = 0; i < 20; i++) {     
            my_record.art2[i] = ttl[i+129];
           }
     my_record.art2[20]=0;
    and then in the last line inserting the null terminator. the file says Artist 2 is a "string 20 characters long" (in BASIC terms) so I made the artist 2 as "char art2[21]" To allow for the null terminator.

    Is this a valid way of making the strings null-terminated for c to handle? (then I'll reverse the process when I get ready to write the files)

    edit: deleted the rest... I accidentally solved my problem... *shrug*
    Last edited by kc8oye; 01-01-2024 at 05:13 PM.

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Text files on a disk, no matter what software created them, are line terminated with either a newline, in UNIX, Linux or modern Mac, '\n', or two characters, a carriage return & newline, "\r\n" in DOS/WIndows. NOT "Null terminated!!!

    When a text line is read into memory, by fgets(), etc..., then the string, in memory, IS automatically Nul terminated by fgets().

    Binary files of any file type, (Other than text files) are a whole different ballgame.

    You need to study a good up-to-date book on the C Programming Language.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Presumably his VB file has fixed-length records, with fields padded with spaces (or with some other character) on the right. So it is essentially a binary file.
    I think he just wants to read these strings into a C program and ensure they are null-terminated (removing the padding) to work as standard C strings.
    I assume he wants to write them back out in the same fixed record format since he mentions "reversing the process". This would entail adding the padding characters back.
    Alternatively he could write them back out as a text file, each field terminated by a newline. The records wouldn't be seekable then, but that would not really be a problem unless the file is very large.

    kc8oye, could you run the following program on your file with a command like:
    $ dumpit data.dat 256
    The $ is your command line prompt; dumpit is whatever you call the dump program; data.dat is your data file; 256 is the number of bytes to dump. The program also allows a third parameter to skip a certain number of bytes first. A COUNT of 0 means to print the whole file. So dumpit data.dat 0 256 would output all but the first 256 bytes.
    Try dumping the first 256 bytes. Then we can see exactly what the data looks like, and in particular what the padding characters are.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
     
    int main(int argc, char **argv) {
        if (argc < 2 || argc > 4) {
            fprintf(stderr, "Usage: %s FILENAME [COUNT [SKIP]]\n", argv[0]);
            return 1;
        }
     
        int count = 0, skip = 0;
        if (argc > 2) {
            count = atoi(argv[2]);
            if (count < 0) count = 0;
            if (argc > 3) {
                skip = atoi(argv[3]);
                if (skip < 0) skip = 0;
            }
        }
     
        FILE *fin = fopen(argv[1], "rb");
        if (!fin) {
            perror("Cannot open input file");
            return 1;
        }
        if (skip > 0) fseek(fin, skip, SEEK_SET);
     
        int c, n = 0;
        char line[17] = {0};
     
        printf("%06X  ", skip);
        while ((count == 0 || n < count) && (c = fgetc(fin)) != EOF) {
            printf("%02X ", c);
            line[n % 16] = isprint(c) ? c : '.';
            if (++n % 16 == 0)
                printf(" |%s|\n%06X  ", line, skip + n);
        }
     
        if (n % 16 != 0) {
            line[n % 16] = '\0';
            printf("%*s |%s|", (16 - n % 16) * 3, " ", line);
        }
        putchar('\n');
     
        fclose(fin);
     
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. null terminated string lists
    By megafiddle in forum C Programming
    Replies: 3
    Last Post: 06-28-2016, 10:09 PM
  2. Non null-terminated strings
    By Casey G in forum C++ Programming
    Replies: 5
    Last Post: 05-10-2011, 07:47 PM
  3. null terminated
    By TeQno in forum C Programming
    Replies: 5
    Last Post: 06-06-2003, 06:10 PM
  4. Null Terminated Arrays
    By sean in forum A Brief History of Cprogramming.com
    Replies: 29
    Last Post: 06-17-2002, 11:39 AM

Tags for this Thread