I want to relean a little C with this simple program
I programed in C 3 decades ago and also C++,
but I have forgotten it. To start with I want to write a very simple program. The command line (followed by example) to call it is: CkNummFld beginCol fieldWidth textfile.dta CkNumFld 71 8 textfile.dta Text file lines end in "\n" but may have the "\r" to ignore. I want to see if the field can be parsed to double type. Some lines may be shorter than begincol+fieldwidth-1, so the field is not parsable. I want to use it to precheck for usage of old Fortran program formats and to start to relearn C. The pseudo code is something like this: int main() { openTheFile; int iLin=0,nOfShortLin=0,nOfZero=0,nOfBlank=0,nOfSmalAbsNon0=0,nOfNonPars=0; for(iLin=0;;iLin++) { luck=readLuck=readLine(inFilStrm,lineBuf,fileStg); if(not luck)break; if(lineBuf.length()<locCol-1+fldWid) { nOfShortLin++; cout << "At line# " << (iLin+1) << " line too short; line:" << lineBuf << endl; continue; } field=lineBuf.substring(locCol-1,locCol-1+fldWid); dblNum=Double.NaN; if(fieldIsAllBlanks(field))nOfBlank++; else { field=fieldTrimmedOfBlanks(field); try { dblNum=Double.parseDouble(field); } catch(NumberFormatException e) { nOfNonPars++; cout << "At line# " << (iLin+1) << " bad field=" << "\"" << field+"\", line:" << lineBuf << endl; } } if(dblNum==0)nOfZero++; else if(Math.abs(dblNum)<=.2)nOfSmalAbsNon0++; } closeTheFile; cout << "Counts: Lines=" << iLin << " (Blank=" << nOfBlank << " Zero=" << nOfZero << " SmalAbs.1="nOfSmalAbsNon0 << ") (NonPars=" << nOfNonPars << " Short=" << nOfShortLin << ")" << endl; } Anything C that does exactly the same job is OK. I don't know what includes, reads, writes, etc to use. This should be easy to finish for someone who knows C/C++. Thanks if this interests you. |
Sorry, it looks like the submit button erases the
indents which readability depends on. Sorry I posted it. |
Quote:
Code:
#include <stdio.h> |
Quote:
You said you want to "start to relearn C" but your sample code uses a lot of C++ features and the task is one that is a lot easier in C++ than in C. Maybe you are better off learning enough C++ for that task, rather than trying to do it in C. Even in C++, you have too much pseudo code and too little actual code for me. Maybe someone else will have more patience, especially after you EDIT or repost with CODE tags. But maybe you should look at a few C++ tutorials first. Most of what you want to do would be somewhere in the first few examples of a typical C++ tutorial. If you use that method to convert most of your pseudo code to valid C++ yourself, you will get better help with any details that you can't find from the beginning of a tutorial. |
All times are GMT -5. The time now is 06:08 PM. |