Thread: file IO and strings

  1. #1
    Unregistered
    Guest

    file IO and strings

    I want to open few files using a for loop so i did:

    for(f=1;f<=3;f++)
    {
    str1="itoa(f)";
    str2="c:\\test";
    str3=".txt";
    strcat(str2,str1);
    strcat(str2,str3); // str=c:\\test1.txt, c:\\test2.txt
    fp= fopen(str2,"r");
    if(fp==NULL)
    {
    printf("\n\ncannot open file\n");
    exit();
    }

    This program gives a lot of errors saying left operand must have a L-value .

    please hep.

    Thaning you in advance.
    Himanshu

  2. #2
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    what are these mythical str1, str2, str3? are they properly initialized? are they null-terminated?

    you can also do this quicker with sprintf btw.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    A few of these lines leave me wondering...

    str1="itoa(f)";

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by quzah
    A few of these lines leave me wondering...

    str1="itoa(f)";

    Quzah.
    hmm yeah, i dont use itoa() so that's why i said use sprintf instead

    looking it up it's not a guaranteed function, although my libc happens to have one, but it takes 3 args and not one

    unregistered: instead of saying "a lot of errors" maybe you could actually read the errors and use your brain to process them? (or even just post them here)

  5. #5
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Don't they have something to join file names like splitpath was used to seperate them?
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  6. #6
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by ronin
    Don't they have something to join file names like splitpath was used to seperate them?
    but this isnt seperating a filename into its path and stuff. anyway unregistered here's code i used in a recent program to do this sort of thing:

    Code:
    /* snip */
    int i;
    char filenom[16];
    FILE *f;
    /* snip */
    for (i = 0; i < 97; i++)
    {
      /* snip */
      sprintf (filenom, "samp%3i.wav", i + 1);
      f = fopen (filenom, "wb);
      /* snip */
      fclose (f);
    }
    /* snip */
    hello, internet!

  7. #7
    Unregistered
    Guest

    it worked

    Thanks moi it worked !!

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by moi


    hmm yeah, i dont use itoa() so that's why i said use sprintf instead

    looking it up it's not a guaranteed function, although my libc happens to have one, but it takes 3 args and not one

    unregistered: instead of saying "a lot of errors" maybe you could actually read the errors and use your brain to process them? (or even just post them here)
    You missed the point. 'itoa()' was in quotes, as such, it's just a string. Look at that line again:

    str1="itoa(f)";

    Now do you see what I was talking about?

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    ... and along the lines of variable types, we don't know what str1, str2 and str3 are defined as.

    If they were pointers, then this:
    str1="itoa(f)";
    str2="c:\\test";
    strcat(str2,str1);
    is wrong.

    If they were arrays, then this:
    str1="itoa(f)";
    str2="c:\\test";
    strcat(str2,str1);
    is wrong.

    Confusion reigns
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by quzah


    You missed the point. 'itoa()' was in quotes, as such, it's just a string. Look at that line again:

    str1="itoa(f)";

    Now do you see what I was talking about?

    Quzah.
    /me does a double take

    oopsie
    hello, internet!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strings and file io
    By barneygumble742 in forum C++ Programming
    Replies: 7
    Last Post: 06-28-2005, 10:27 AM
  2. Multithreading and File IO
    By sean in forum C# Programming
    Replies: 1
    Last Post: 10-18-2004, 12:36 AM
  3. File IOStreams
    By Maghappy in forum C++ Programming
    Replies: 4
    Last Post: 09-19-2001, 10:02 PM
  4. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM
  5. file io ?
    By cozman in forum C++ Programming
    Replies: 3
    Last Post: 08-10-2001, 01:26 PM