Thread: string/file help needed

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    6

    Question string/file help needed

    I have been out of C programming for a while and can not seem to get this to work. I have looked over the net and through my books, but I must be doing something wrong. I have a file (ascii) that looks like

    # #
    #
    12 345 21354 0.03425 0.2435 4 843
    #
    #
    # #
    # #
    # #
    ....
    1.2425e-5 4.2345e-3 5.1234e-7 2.3452e-6 2.2134e-5
    .....

    the # marks are some number, I don't care.
    I need to pull the 3rd line of the file out, and
    print out only the 3rd, 4th and 6th numbers, then
    go to the numbers in sci-notation and print out
    every _other_ one... there are about 400 rows
    of these...

    I started off on the wrong foot and ended up scrapping that code, so I am starting over. I think that if I can get past my dismal situation here I may be able to proceed on my own. What I have so far is:
    #include <stdio.h>
    #include <ctype.h>
    #include <stdlib.h>
    #define BUFSIZE 1024
    main()
    {
    unsigned i=0;
    int j,si=0;
    char string[BUFSIZE];
    char tempstring[BUFSIZE];
    FILE *input;
    input=fopen("input.avg", "r");
    if (input != NULL)
    {
    printf("the contents are...\n");
    while(fgets(string, BUFSIZE, input) != NULL) {
    if (i==2){
    char *phrase=string;
    char *ts=tempstring;
    }
    if (isspace(atoi(phrase))){
    j++;
    }
    if ((j>=2 && j<=3)||(j>5 && j<6)){
    ts[si]=phrase;
    si++;
    }
    phrase++
    }
    }
    fputs(ts, stdout);
    fputs(string, stdout);
    i++;
    }
    }

    }

    And this no workie. (well, it did until I tried to get that 3rd line thing to work.) I have some other code I was working on, but it is long and painful. I need to output this to a file, but I think I could handle that... I also need the sci-notation numbers to be in float format, but I think I can handle that myself also... I just can not seem to get back into the string swing of things.

    Any help would be appreciated. You can slap me around for not recalling how to deal with strings also

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Hmm... can you explain what each line of code is supposed to do, perhaps commented in plain English? If so, I will try to help, but as is, the code is jumbled and unclear...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    6
    Ok, sorry about that....

    #include <stdio.h>
    #include <ctype.h>
    #include <stdlib.h>
    #define BUFSIZE 1024
    main()
    {
    unsigned i=0;
    int j,si=0;
    char string[BUFSIZE]; /* create space for file*/
    char tempstring[BUFSIZE]; /* temp string */
    FILE *input; /* pointer to file input *
    input=fopen("input.avg", "r"); /*open for read*/
    if (input != NULL) /* check for empty file*/
    {
    printf("the contents are...\n");

    /* read in file one line at a time*/
    /* store each line in "string" */

    while(fgets(string, BUFSIZE, input) != NULL) {

    /* if the line I am on is the second line*/
    /* I want to get the 3rd, 4th and 6th numbers*/
    /* from that line...*/
    if (i==2){
    char *phrase=string; /*pointer to string*/
    char *ts=tempstring; /*ptr to temp string*/
    }

    /* I go about finding the 3rd, 4th, 6th numbers*/
    /* in the line by checking spaces... is this */
    /* char we are on a space?? */
    if (isspace(atoi(phrase))){
    j++;
    }

    /* ok, if it was a space, was it the one I */
    /* am looking for?? If so, I want to put the */
    /* numbers after the space in a temp string */
    /* so that I can print them out later on...*/
    if ((j>=2 && j<=3)||(j>5 && j<6)){
    ts[si]=phrase;
    si++;
    }
    phrase++ /* advance on char in string*/
    }
    }
    fputs(ts, stdout); /*print out to check*/
    fputs(string, stdout); /*print each line */
    /*just to see...*/

    /* i tells me which line I am on*/
    i++;
    }
    }
    }

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >>>>if (isspace(atoi(phrase))){
    This cannot possibly work. "phrase" is a being converted to an integer and then tested for being a space or not!

    >>>>if ((j>=2 && j<=3)||(j>5 && j<6)){
    This is shear nonsense. How can "j" be two numbers at once(in the first part)? And the second part is equal nonsense since the condition will never be met? You should completely scrap this...



    ************************************************
    You should probably use fread() to read the file into your buffer.

    To simplify things, you should first determine the "metrics" of the buffer(how many newlines, etc) and go from there.

    You might even consider reading contents of buffer into an array, which would make the program more robust, stable, and manageable, to say the least. But it looks to me you took the "code first" approach (a disaster for newbies like us) which almost never yield a satisfactory result.

    But as I said, I am willing to help, but please try to explain your logic first...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    The char pointers phrase and ts are local to their if conditional block. If you want to use them outside of this block they'll have to be declared outside of it.

    Also (j>5 && j<6) will never produce a true outcome.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >>>>while(fgets(string, BUFSIZE, input) != NULL) {

    O.k., fgets() will stop reading in input at the fist line. So you need to use "fread()" to get everything in the buffer.

    Look:

    PHP Code:
      fseek(input,0L,SEEK_END);  
    //set the file pointer to the end of the file you opened

      
    size_t  bufsize ftell(input);
    //bufsize will now = the file size in bytes

      
    rewind(input);  // reset file pointer to beginning of file

      
    char string[bufsize]; 
    // "string" will now automatically be the right size

      
    fread(string,bufsize,1,input);  //read file into the buffer

      
    fclose(input);    //done with file, close it now 
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    6

    well...

    I was going to go along with the "read it
    into an array" idea, but I have been trying that and I think I am a retard at it...

    This is what I now have...

    #include <stdio.h>
    #include <ctype.h>
    #include <stdlib.h>
    #include <string.h>
    #define BUFSIZE 1024
    main()
    {
    unsigned Linecount=0;
    int j,spacecount=0;
    char string[BUFSIZE];
    char tempstring[BUFSIZE];
    char tempstring2[BUFSIZE];
    char *ary_ptr=&tempstring[0];
    char *ary_ptr2=&tempstring2[0];
    int ok=1;

    FILE *input;
    input=fopen("input.avg", "r");
    if (input != NULL)
    {
    printf("the contents are...\n");
    while(fgets(string, BUFSIZE, input) != NULL) {
    if (Linecount==2){
    strcpy(tempstring, string);
    while(ok){
    if(isspace(atoi(*ary_ptr))){
    spacecount++
    }
    if((spacecount==2)||(spacecount==3)||(spacecount== 5)){
    (*ary_ptr2+i)=(*ary_ptr+i);
    }
    if(*ary_ptr='\n'){
    ok=0;
    }
    i++
    }
    fputs(tempstring2, stdout);
    fputs(string, stdout);
    Linecount++;
    }
    }

    }

    Originally I tried an attempt that was like what Sebastini pointed out, but then I could not figure out how to read a line, manipulate it (ie. take out what I wanted only) and then put it in a file...

    hmph.

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    6

    ops, wait up...

    I spoke too soon.. I have an old copy (the first way I was trying this) and did it as per Sebastiani's code...
    It works, and I can print out the file with the sci-notations converted, but I can not seem to be able to manipulate individual lines...

    e.g. I want to take like 2 and only print some of the numbers to a file, then pass all of the other numbers until I get to the sci notation numbers and then print those out as floats...

    I have attached the orig attempt to this msg for viewing with comments...

    Yes, this is a hack and slash attempt from other code...

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    O.k., now you have a buffer which contains the file. Now you make a choice:

    1)you can either loop through buffer till it passes the correct amount of newlines(by incrementing an int...), and then nest more loops to find the data you need

    2)you could automate things a bit and create an array of strings, and using a loop, fgets each line into the array. This would allow you more control and certainty, but either way would work. If you do it this way though, first send "string" through a loop and count the newlines. Then you know how big the array needs to be. At that point you can read the contents into the the array with a loop. After that, it's a simple matter of manipulation to do the rest.

    *********************************************
    >>>>if(isspace(atoi(*ary_ptr))){

    Again: This will not work. Get rid of the "atoi" and put some brackets at the end of ary_ptr --> "if(isspace(ary_ptr[i]))...."(Tho "i" would not fit into your code at present)




    I don't want to sound negative, but, as is, your code is a mess!

    You should try to do things more methodically. Codewriting is part simple logic / part syntax, etc.... Your code lacks both! You are trying to do too much too quickly without a solid basis. And what if your requirements change(the data file format changes)? Or what if you later need more/ different data from this file? Your aim should be to make it to where you can manipulate the data any way you choose without changing much. Your current approach would not accomadate easily any change...

    I have only been programming for 5 months. When I started, I was most concerned with simply "getting it to work" in a similar fashion as yours. Now I try to instead be more methodical and modular in my coding which makes debugging very easy.

    Anyway, keep at it, and try to keep it simple.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  10. #10
    Registered User
    Join Date
    Sep 2001
    Posts
    6

    Exclamation ok, try read strings to array

    Ok, as mentioned, since I have the file in a buffer, and from the code I attached, I have
    the number of lines in the file, I will try to read this into an array...

    I am supposing that I need to create an array of strings...?? Like
    char mystrings[Linecount]
    then make a pointer to that to manipulate them?
    I thought this would create an array of chars, and each array cell could only hold a single char??

    Thanks for the help so far....

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >>>>mystrings[Charcount]
    O.K., this is an array of chars.

    mystrings[Linecount][Charcount]
    This is an array of strings, each one being "Charcount" long

    So first count newlines, like I said before.

    Then loop again to find the max number of chars in each string. Like this:

    PHP Code:
      int len=strlen(string);
      
    char newline='\n';     //stop counting char at a '/0' too...
      
    int count_max=0;
      
    int max_sz=0;
      for(
    int d=0;d<len;d++)
        {
               if(
    isalpha(string[d]) || isdigit(string[d]) || isspace(string[d]) && string[d] != newline)
                {
                 
    count_max++;
                           
                         if(
    count_max max_sz)
                               {
                                
    max_sz=count_max;  
                               }
                 }
            else {
                  
    count_max=0;  // reset to count the others
                 
    }
        } 
    'max_sz' is now the length of your longest string.

    Now, assuming you have figured out how to count your newlines, let's just call the number of newlines "max_num". So now we create and initialize the array of strings.

    PHP Code:
    char line[max_num][max_sz+3]; //+3 for safety

    for(int h=0h<=max_num-1h++) //-1 because arrays start @ '0'
      
    {
       
    fgets(line[h],max_sz+2,string); //+2 for safety
      

    Voila! An array of strings. Simple,huh?

    But that's just the beginning. Now you have to pull the data from them!

    To make an array of pointers to those strings you would simply do:

    PHP Code:
    char *line_ptrs[max_num];

    for(
    int y =0y<=max_num-1y++){ line_ptrs[y] = line[y]; } 
    Last edited by Sebastiani; 09-06-2001 at 02:07 PM.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  12. #12
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Also, you should encapsulate these routines into functions
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  13. #13
    Registered User
    Join Date
    Sep 2001
    Posts
    6
    My compiler (gcc) pukes on the char line[max...

    parse error... hmm. Can't seem to
    figure out what is causing that...

    [QUOTE]Originally posted by Sebastiani
    [B]>>>>mystrings[Charcount]


    [/php]
    'max_sz' is now the length of your longest string.

    Now, assuming you have figured out how to count your newlines, let's just call the number of newlines "max_num". So now we create and initialize the array of strings.

    PHP Code:
    char line[max_num][max_sz+3]; //+3 for safety

    for(int h=0h<=max_num-1h++) //-1 because arrays start @ '0'
      
    {
       
    fgets(line[h],max_sz+2,string); //+2 for safety
      

    Voila! An array of strings. Simple,huh?

  14. #14
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Whoops, I don't think you can "fgets()" from a string. Reopen the file, and fgets() from there.

    My compiler (gcc) pukes on the char line[max...
    Parse error usually means you forgot a punctuation mark somewhere...

    How did you end up figuring out the value of "max_num"? Maybe that has something to do with it...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. free needed or not?
    By quantt in forum Linux Programming
    Replies: 3
    Last Post: 06-25-2009, 09:32 AM
  2. C Programmers needed for Direct Hire positions
    By canefan in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 09-24-2008, 11:55 AM
  3. lock needed in this scenario?
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-25-2008, 07:22 AM
  4. C++ help needed
    By Enkindu in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 08-31-2004, 11:24 PM
  5. error with function, help needed quick
    By chris285 in forum C++ Programming
    Replies: 3
    Last Post: 04-30-2003, 08:31 AM