Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

fgets appears to read beyond newline

Snoopy Dog
2012-02-29
2012-09-26
  • Snoopy Dog
    Snoopy Dog
    2012-02-29

    Newbie here trying to do a file conversion. Testing with a small file and I
    only get one line read in.

    I have taken most of the code from samples I found on the internet, and I
    admit this is a Brute Force and Ignorance compilation. As the code will show,
    I am using printf debugging....

    Any help greatly appreciated.

    Here is the code...

    include <windows.h>

    include <stdlib.h>

    include <stdio.h>

    include <string.h>

    WIN32_FIND_DATA *fd;

    //int main(int argc, char argv)
    int main(void)
    {
    char
    sourcedir = "c:\tsource\";
    char *outputdir = "c:\toutput\";
    char sourcefilename = "\0";
    char outputfilename = "\0";
    char listfilename = "\0";
    HANDLE fh;
    char tmppath = "\0";
    char fnamesource = "\0";
    char fnameoutput = "\0";
    char sourcename = "\0";
    char outputname = "\0";
    char lineout = "\0";
    char linein = "\0";
    char data;
    int pass, returncode;

    fd = malloc(sizeof(WIN32_FIND_DATA));
    strcat(tmppath, sourcedir);
    strcat(tmppath, "");
    printf("\nScanning: %s\n", tmppath);
    fh = FindFirstFile((LPCSTR) tmppath,fd);
    if(fh != INVALID_HANDLE_VALUE)
    {
    do
    {
    if(fd->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    { // this shoud be for Directories only - bypass
    printf("Directory\n");
    }
    else
    {
    fnamesource = '\0';
    fnameoutput = '\0';
    pass=0;
    strcat(fnamesource, sourcedir);
    strcat(fnamesource, fd->cFileName);
    strcat(sourcename, fd->cFileName);
    sprintf(outputname, "%.
    s", strcspn(sourcename, "."), sourcename);
    printf("File opening is %s\n", fnamesource);
    FILE outputfile = fopen(fnamesource, "r");
    fclose(outputfile);
    FILE
    sourcefile = fopen(fnamesource, "r"); / try to open the file /
    if ( sourcefile == NULL )
    {
    printf("Failed to open %s\n", fnamesource);
    break;
    }
    else
    {
    //printf("Opened Source %s\n", fnamesource);
    / Now read the line of data in/
    while (fgets(linein, sizeof linein, sourcefile) != NULL)
    {
    printf("Reading Input Line: %s of length %d chars\n", linein, strlen(linein));
    size_t i = 0, size;
    char token = linein;
    for ( ;; )
    {
    //printf("for loop\n");
    data_ = '\0';
    size_t len = strcspn(token, ",\n");
    sprintf(data_, "%.
    s", (int)len, token);
    token += len;
    if ( *token == '\0')
    {
    printf("Break due to EOS\n");
    break;
    }
    ++token;
    printf("Input data: %s token: %d length %d \n", i, data_, token, len);

    if (++i >= sizeof data / sizeof data)
    {
    printf("PROBLEM\n");
    break;
    }
    }
    if (pass==0)
    {
    fnameoutput ='\0';
    strcat(fnameoutput, outputdir);
    strcat(fnameoutput, data);
    strcat(fnameoutput, ".csv");
    printf("OUTPUT File opening is %s\n", fnameoutput);
    FILE
    outputfile = fopen(fnameoutput, "w+");
    if (outputfile == NULL)
    {
    break;
    }
    }
    strcat(lineout, data);
    strcat(lineout, ",\"");
    strcat(lineout, outputname);
    strcat(lineout, "\",");
    strcat(lineout, data);
    strcat(lineout, ",");
    strcat(lineout, data);
    strcat(lineout, ",");
    strcat(lineout, data);
    strcat(lineout, ",");
    strcat(lineout, data);
    strcat(lineout, ",");
    strcat(lineout, data);
    strcat(lineout, ",");
    strcat(lineout, data);
    returncode = fputs(lineout, outputfile);
    fflush(outputfile);
    printf("Data written is : %s\n", lineout);
    if (returncode == 0)
    {
    printf("WRITE LINE OF DATA TO OUTPUT FILE on Pass %i with return code %d\n",
    pass, returncode);
    }
    else
    {
    printf("failed to WRITE LINE OF DATA TO OUTPUT FILE %i\n", returncode);
    }
    // get the next line of data from source file
    lineout = '\0';
    linein = '\0';
    data = '\0';
    data = '\0';
    data = '\0';
    data = '\0';
    data = '\0';
    data = '\0';
    data = '\0';
    data = '\0';
    data = '\0';
    ++pass;
    printf("\n\n");
    }
    fflush(outputfile);
    fclose(outputfile);
    fclose(sourcefile);
    }
    fnamesource = '\0';
    }
    }
    while(FindNextFile(fh,fd));
    }
    FindClose(fh);
    system("PAUSE");
    return 0;
    }

    Here is a sample data file:

    CNC5,1/1/2012,5100.54,5100.54,5100.54,5100.54,100
    CNC5,1/2/2012,5200.22,5200.22,5200.22,5200.22,200
    CNC5,1/3/2012,5300.33,5300.33,5300.33,5300.33,300
    CNC5,1/4/2012,5400.44,5400.44,5400.44,5400.44,400
    CNC5,1/5/2012,5500.44,5400.44,5400.44,5400.44,500
    CNC5,1/6/2012,5600.44,5400.44,5400.44,5400.44,600
    CNC5,1/7/2012,5700.44,5400.44,5400.44,5400.44,700
    CNC5,1/8/2012,5800.44,5400.44,5400.44,5400.44,800
    CNC5,1/9/2012,5900.44,5400.44,5400.44,5400.44,900
    CNC5,1/10/2012,5000.44,5400.44,5400.44,5400.44,1000
    CNC5,1/11/2012,5100.44,5400.44,5400.44,5400.44,1100
    CNC5,1/12/2012,5200.44,5400.44,5400.44,5400.44,1200
    CNC5,1/13/2012,5300.44,5400.44,5400.44,5400.44,1300
    CNC5,1/14/2012,5400.44,5400.44,5400.44,5400.44,1400
    CNC5,1/15/2012,5500.44,5400.44,5400.44,5400.44,1500
    CNC5,1/16/2012,5600.44,5400.44,5400.44,5400.44,1600
    CNC5,1/17/2012,5700.44,5400.44,5400.44,5400.44,1700
    CNC5,1/18/2012,5800.44,5400.44,5400.44,5400.44,1800
    CNC5,1/19/2012,5900.44,5400.44,5400.44,5400.44,1900
    CNC5,1/20/2012,5000.44,5400.44,5400.44,5400.44,2000


     
  • Snoopy Dog
    Snoopy Dog
    2012-02-29

    I should add that the Output file is created, but there is NO DATA in it.

    Also, using Dev-Cpp 4.9.9.2. Build as a Console application, using C.

    Running on Vist 64 bit OS.

    Let me know if any other info needed.

    TIA

    Snoopy

     
  • Snoopy Dog
    Snoopy Dog
    2012-02-29

    I increased the size of my test file to 99 lines, and it reads in line 1 (as
    expected) and then reads PART of line 78.

    No other lines are read in.

    I don't know of an easy way to determine the number of characters that it has
    moved from opening the file to where the first fgets seems to leave the next
    read indicator.

    Again, ANY help is GREATLY appreciated.