Announcement

Collapse
No announcement yet.

need urgent help with understanding a little c code

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • need urgent help with understanding a little c code

    Hi

    I need to get this short c-code working for a colleague. He has compiled it to work on his mac. You can see the source at http://www.physionet.org/physiotools/dfa/dfa.c and you can read about the program at http://www.physionet.org/physiotools/dfa/dfa-1.htm

    I know virtually nothing about c and would appreciate some guidance.

    I have replaced the code that reads the command line arguments with printf & scanf instructions to prompt for inputs and this works fine.

    Then we come to the difficult bit: We want to read in a list of data from a text file. Currently it uses the 'standard input' which I think means the keyboard. The program keeps looping around accepting values, but we can't seem to terminate the list input. This is the line in question: while (scanf("%lf", &y) == 1) { ...


    Can someone please point me in the right direction? Initially to get the keyboard input working, and possibly to read a file instead?

    Many thanks in advance.

    Tony.
    FT.

  • #2
    I'm a little rusty, but to get you started:
    Standard input is just about any stream directed at the program.

    Try Ctrl-Z to end the input from a keyboard
    or "dfa < input.txt" to use input.txt as a stream source
    or "type input.txt | dfa.exe" to pipe the text from the file into the program //this one won't work if it contains much but regular text.

    <i see invizo-text is still working>
    chuck
    Last edited by cjolley; 26 July 2002, 08:41.
    Chuck
    秋音的爸爸

    Comment


    • #3
      Thanks Chuck.

      He's running OS9.1 and I think just wants to start the compiled code and then type in the name of the data file - that's why he had me mod the command line arguments into prompts.

      The input is only a stream of numbers, as are the arguments now, so I guess we could create a file that contains the arguments and the data and give your suggestions a shot! Presumably icon shortcuts on macs can be modified in the same way as on PCs?

      Thanks again

      Tony.
      FT.

      Comment


      • #4
        Here is a skeleton example of how to open & read a file line by line.
        Assuming each data point is on a separate line in the file & not realy a stream.

        Code:
        #include &ltmalloc.h&gt 
        #include &ltstdio.h&gt
        
        #define BUFFSIZE 256
        
        int skeleton_input(char* FILE_NAME)
        {
        	FILE  *InFile;
        	char  *linebuf;
        
        	if ((InFile = fopen(FILE_NAME, "r")) != NULL)
        	{
        		linebuf = calloc(BUFFSIZE + 1, 1);
        		while(fgets(linebuf, BUFFSIZE, InFile))
        		{
                                              STICK THE DATA IN THE ARRAY HERE
        		}
        		fclose(InFile);
        		free(linebuf);
        	}
        	else
        	{
        		printf("%s%s", "Can't open ", FILE_NAME);
        	}
        	return 0;
        }
        chuck

        PS Surely someone less rusty than me is learking around here somewhere.

        EDIT - thanks KeiFront


        Last edited by cjolley; 26 July 2002, 13:31.
        Chuck
        秋音的爸爸

        Comment


        • #5
          Re: need urgent help with understanding a little c code

          This is the line in question: while (scanf("%lf", &y) == 1) { ...
          This line will read in numeric data until a character or other symbol is detected (e.g. 5 5.63 6 9 8 10 A).



          if ((InFile = FILE_NAME, "r")) != NULL)

          should be

          if (InFile = fopen(FILE_NAME, "r")) != NULL)
          Main: Dual Xeon LV2.4Ghz@3.1Ghz | 3X21" | NVidia 6800 | 2Gb DDR | SCSI
          Second: Dual PIII 1GHz | 21" Monitor | G200MMS + Quadro 2 Pro | 512MB ECC SDRAM | SCSI
          Third: Apple G4 450Mhz | 21" Monitor | Radeon 8500 | 1,5Gb SDRAM | SCSI

          Comment


          • #6
            Re: Re: need urgent help with understanding a little c code

            Originally posted by KeiFront


            This line will read in numeric data until a character or other symbol is detected (e.g. 5 5.63 6 9 8 10 A).



            if ((InFile = FILE_NAME, "r")) != NULL)

            should be

            if (InFile = fopen(FILE_NAME, "r")) != NULL)

            Heh.
            I was hurridly cuting & pasteing from a larger program so as not to be confusing.
            chuck
            Chuck
            秋音的爸爸

            Comment


            • #7
              Thanks for the suggestions guys. Much appreciated.

              T.
              FT.

              Comment


              • #8
                Usually Ctrl-D is the way to type the EndOfFile marker.
                Gigabyte P35-DS3L with a Q6600, 2GB Kingston HyperX (after *3* bad pairs of Crucial Ballistix 1066), Galaxy 8800GT 512MB, SB X-Fi, some drives, and a Dell 2005fpw. Running WinXP.

                Comment


                • #9
                  Shouldn't that be:

                  if ((InFile = fopen(FILE_NAME, "r")) != NULL)

                  ???

                  - Gurm
                  The Internet - where men are men, women are men, and teenage girls are FBI agents!

                  I'm the least you could do
                  If only life were as easy as you
                  I'm the least you could do, oh yeah
                  If only life were as easy as you
                  I would still get screwed

                  Comment


                  • #10
                    Wombat- In a dos box on XP ctrl-z works, I tried it
                    Is ctrl-D a Mac thing?

                    GURM- That's what I changed the code to.

                    Tony- Look up, I changed the code.

                    chuck


                    PS It's amazing what can distract one from work
                    Chuck
                    秋音的爸爸

                    Comment


                    • #11
                      How the heck do you show a <<>>s for an include?
                      They keep vanishing.
                      chuck
                      Chuck
                      秋音的爸爸

                      Comment


                      • #12
                        The HTML code is (ampersand)lt and (ampersand)gt

                        &lt&lt&gt&gt
                        Gigabyte P35-DS3L with a Q6600, 2GB Kingston HyperX (after *3* bad pairs of Crucial Ballistix 1066), Galaxy 8800GT 512MB, SB X-Fi, some drives, and a Dell 2005fpw. Running WinXP.

                        Comment


                        • #13
                          &ltI have no idea &lt&gt what you're talking &lt&gt about!&gt

                          - Gurm
                          The Internet - where men are men, women are men, and teenage girls are FBI agents!

                          I'm the least you could do
                          If only life were as easy as you
                          I'm the least you could do, oh yeah
                          If only life were as easy as you
                          I would still get screwed

                          Comment


                          • #14
                            Ahh.... much better

                            Tony- look up, I changed the code again

                            chuck

                            PS Why doesn't the html parser ignore that stuff untill it gets to the /code tag?
                            Chuck
                            秋音的爸爸

                            Comment


                            • #15
                              Almost better, Try cheating. After the lt, add an HTML comment < ! >

                              The UBB parser is funky.
                              Gigabyte P35-DS3L with a Q6600, 2GB Kingston HyperX (after *3* bad pairs of Crucial Ballistix 1066), Galaxy 8800GT 512MB, SB X-Fi, some drives, and a Dell 2005fpw. Running WinXP.

                              Comment

                              Working...
                              X