LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-26-2011, 01:08 PM   #1
hill0093
LQ Newbie
 
Registered: Jul 2005
Posts: 12

Rep: Reputation: 0
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.
 
Old 01-26-2011, 02:54 PM   #2
hill0093
LQ Newbie
 
Registered: Jul 2005
Posts: 12

Original Poster
Rep: Reputation: 0
Sorry, it looks like the submit button erases the
indents which readability depends on.
Sorry I posted it.
 
Old 01-26-2011, 03:00 PM   #3
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by hill0093 View Post
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() */
 
Old 01-26-2011, 03:40 PM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by hill0093 View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple C++ Program: Program Compiles But Won't Run (Segmentation Fault) violagirl23 Programming 3 01-09-2008 12:09 AM
simple program help! rohitkara Linux - General 1 05-24-2005 08:57 PM
simple c program liguorir Linux - Software 4 05-29-2004 06:22 AM
simple program bprasanth_20 Programming 5 10-29-2003 12:55 AM
Help with the following simple C program ..... purpleburple Programming 5 09-13-2002 10:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration