I want to relean a little C with this simple program
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
When you post code, paste it in there just as you did. Then highlight the code, and click on the octothorpe ("#") above the editing window. If you do so, you get nice preservation of indentation, thus:
Sorry, it looks like the submit button erases the
indents which readability depends on.
Sorry I posted it.
wje_lq told you a way to use CODE tags, so now you know how to avoid losing all the formatting of code. You also should know how to use the EDIT button. When you see your post doesn't look as you intended, you can edit it instead of apologizing for it.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.