Thread: loop passing int to char

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    1

    loop passing int to char

    I am new to C++, and I would be very grateful if you could help me out.

    I've a series of files from which I have to extract data from. They have similar names, the only thing that differs is a number, for example file1.txt, file2.txt..., up to 50.000.

    I've already defined a code that passes a number (from a counter) to a char, that is later concatenated, giving me the file name that is used in a procedure to extract the data. As showed in the code below.

    But after 5 loop iterations the program is not able to open the files anymore and it says that the program has encoutered an error and needs to close. What is wrong with my code? How can I make it work?

    Many thanks for your help.

    Code:
    int main()
    {
          char  buf[BUFSIZ];
          int   i;
          char  name[] = "HIV_HPV_calib.pkg_Global_";
          char  end []= ".txt";
    
          for (int gh=1; gh <51; gh++)
          {
             i = gh;
    
            snprintf(buf, sizeof(buf), "%d", i);
    
            snprintf(buf, sizeof(buf), "%s%d%s", name, i , end);
    
    
            extract_file_vector (buf);
            ...
          }
    }
    Last edited by Salem; 07-21-2010 at 10:29 PM. Reason: Fixed [code][/code] tags - learn to use them yourself

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    [CODE][/CODE] go AROUND the code.
    Don't just randomly add stuff just to make an error message go away. THINK about what it is telling you and act accordingly.

    If you take the same approach with writing code (just hacking until the error messages go away), it's gonna be a long struggle for you.

    Did you read any other post?
    Did you see nicely formatted code?
    Did YOUR post look like that before I edited it?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Producer/Consumer - problems creating threads
    By hansel13 in forum C Programming
    Replies: 47
    Last Post: 08-20-2010, 02:02 PM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Replies: 26
    Last Post: 11-30-2007, 03:51 AM
  4. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM