LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   I want to relean a little C with this simple program (https://www.linuxquestions.org/questions/programming-9/i-want-to-relean-a-little-c-with-this-simple-program-858843/)

hill0093 01-26-2011 01:08 PM

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.

hill0093 01-26-2011 02:54 PM

Sorry, it looks like the submit button erases the
indents which readability depends on.
Sorry I posted it.

wje_lq 01-26-2011 03:00 PM

Quote:

Originally Posted by hill0093 (Post 4238644)
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:
Code:

#include <stdio.h>

int main(int    argc,
        char **argv
        )
{
  if(argc>1)
  {
    printf("%s\n",
          argv[1]
          );
  }
  else
  {
    printf("Hello, world!\n");
  }

  return 0;
 }

} /* main() */


johnsfine 01-26-2011 03:40 PM

Quote:

Originally Posted by hill0093 (Post 4238644)
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.


All times are GMT -5. The time now is 06:08 PM.