read from a txt file multiple entries and assign to variables

This is a discussion on read from a txt file multiple entries and assign to variables within the C Programming forums, part of the General Programming Boards category; Im learning C coding and have successfully used the code below to read from a txt file the single entry ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    2

    read from a txt file multiple entries and assign to variables

    Im learning C coding and have successfully used the code below to read from a txt file the single entry HSI0 or HSI1 and assign the one or zero to the variable CommandDirection.

    Code:
    LE * pFile;  
        pFile = fopen ("R:\\SierraChart\\Commands.txt","r");
        if (pFile!=NULL) {
            // obtain file size:
            long lSize;  char filetxt[40]=""; char out[1];
            fseek (pFile , 0 , SEEK_END);
            lSize = ftell (pFile);
            rewind (pFile);
            fread (filetxt,1,lSize,pFile);
            fclose (pFile);
            
            SCString InputString(filetxt), HSISymbol("HSI"); 
            
            if(InputString.CompareNoCase(HSISymbol,3)==0) {out[0]=filetxt[3]; CommandDirection=atoi(out);}
        }
    I'm trying to expand that one entry to multiple entries, in order to assign to multiple variables.

    The code below doesn't work, but perhaps will give an idea what I'm trying to do.

    new format in txt file entries:
    HSI1
    TESTA2
    TESTB3

    proposed enhancement:
    Code:
    SCString InputString(filetxt), HSISymbol("HSI");
    SCString InputString2(filetxt), testSymbolA("TESTA");
    SCString InputString3(filetxt), testSymbolB("TESTB");
    
    if(InputString.CompareNoCase(HSISymbol,3)==0) {out[0]=filetxt[3]; CommandDirection=atoi(out);}
    if(InputString2.CompareNoCase(testSymbolA,3)==0) {out[0]=filetxt[3]; CommandDirection2=atoi(out);}
    if(InputString3.CompareNoCase(testSymbolB,3)==0) {out[0]=filetxt[3]; CommandDirection3=atoi(out);}
    so result would be
    CommandDirection=1
    CommandDirection2=2
    CommandDirection3=3

    I am missing some parsing and assignment magic, please help.
    There may be some Sierra Chart coding ACSIL specific functions in the above code, but I'm not positive.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Well char out[1] isn't large enough to hold your string, there is no room for a \0.

    So perhaps your atoi() is processing garbage.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    2
    OK thanks I found an solution here
    Enable/disable button? - Sierra Chart

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to read text file into variables
    By Ankur Arora in forum C Programming
    Replies: 1
    Last Post: 05-11-2011, 01:26 AM
  2. read multiple lines from a file
    By YankeePride13 in forum C Programming
    Replies: 2
    Last Post: 11-10-2005, 09:30 PM
  3. Multiple Read File Descriptors
    By blue8173 in forum C Programming
    Replies: 9
    Last Post: 04-21-2004, 01:19 PM
  4. Read variables and stuff from a text file
    By ChrisJ in forum C++ Programming
    Replies: 1
    Last Post: 06-26-2003, 11:06 AM
  5. multiple entries
    By fizz_uk83 in forum C Programming
    Replies: 2
    Last Post: 12-26-2002, 04:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21