Couple Of Questions I'm Absolutly Stuck On

This is a discussion on Couple Of Questions I'm Absolutly Stuck On within the C Programming forums, part of the General Programming Boards category; Hey, ok lets start with the one your going to find dead easy. i am trying to assign a string ...

  1. #1
    Registered User chriscolden's Avatar
    Join Date
    Jan 2006
    Posts
    32

    Couple Of Questions I'm Absolutly Stuck On

    Hey,

    ok lets start with the one your going to find dead easy.

    i am trying to assign a string to this char array over multiple lines however I cannot work out the syntax

    Code:
    char buffer[500];
    buffer = "\n |-------------------------------------------------------|
    _ \n | Algorithem        | Time    | Comparisons | Swaps     | 
    _ \n |-------------------------------------------------------|";
    2nd question is redarding file access....
    I am trying to create a new text file for logging purposes each time the program runs. So i thought the time would be a nice almost unique filename to have.....again cannot get it to go. Here is the code I have so far.

    Code:
    out=fopen(asctime(time(NULL)) + ".txt","w");
    I'm guessing both are just me not access the char arrays (strings) propperly.

    Thanks for your help in advance.

    Chris
    I'm a noob when it comes to C and I have to use Borland C++ 3.1 because my university won't move on.

    However I do use Turbo C++ 3.1 for windows. Makes life abit less stressful.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,525
    1. With long strings like these declare it on multiple lines by typing \ and then [enter].
    Code:
    buffer = "\n |-------------------------------------------------------| \
     		_ \n | Algorithem        | Time    | Comparisons | Swaps     |  \
    		_ \n |-------------------------------------------------------|";
    2. Strings do not concatenate with plus in C, use the strncat function.
    Code:
    out = fopen( strncat(asctime(time(NULL)), ".txt", 4), "w" );
    Last edited by whiteflags; 06-06-2006 at 09:24 AM.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Adjacent string literals are concatenated.
    Code:
    char buffer[500] = "\n |-------------------------------------------------------|"
                       "_ \n | Algorithem        | Time    | Comparisons | Swaps     | "
                       "_ \n |-------------------------------------------------------|";
    [edit]Note that this and citizen's version are different -- the leading whitespace in citizen's is significant.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by citizen
    2. Strings do not concatenate with plus in C, use the strncat function.
    Code:
    out = fopen( strncat(asctime(time(NULL)), ".txt", 4), "w" );
    I don't think it's a very good idea to concatenate to a static buffer that is part of the standard library and likely has a size fixed exactly for the intended output only.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User chriscolden's Avatar
    Join Date
    Jan 2006
    Posts
    32
    Thanks guys,

    I got it accepting the multi lined string as far as i know but now its saying lvalue required

    Code:
    void printHeader( FILE * filePointer ) {
    	char buffer[500];
    
    	buffer = "\n |-------------------------------------------------------|"
    		"\n | Algorithem        | Time    | Comparisons | Swaps     |"
    		"\n |-------------------------------------------------------|";
    	
    	printf( "%s", buffer );
    	fprintf( filePointer, "%s" );
    }
    Thanks for the quick reply before

    Chris
    I'm a noob when it comes to C and I have to use Borland C++ 3.1 because my university won't move on.

    However I do use Turbo C++ 3.1 for windows. Makes life abit less stressful.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Note that my version uses an initialization. Your uses assignment. You cannot assign an array, but you can initialize it. That is,
    Code:
    char buffer[500] = "\n |-------------------------------------------------------|"
    		"\n | Algorithem        | Time    | Comparisons | Swaps     |"
    		"\n |-------------------------------------------------------|";
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User chriscolden's Avatar
    Join Date
    Jan 2006
    Posts
    32
    thanks for your help guys, I have sorted them problems and now encountered some more, I will try and work them out before posting again.

    cheers
    I'm a noob when it comes to C and I have to use Borland C++ 3.1 because my university won't move on.

    However I do use Turbo C++ 3.1 for windows. Makes life abit less stressful.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    fprintf( filePointer, "%s" );
    You forgot to supply a parameter here.


    [Why put the newline at the beginning? I've never understood that. Put the end of the line at the end of the line...]
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User chriscolden's Avatar
    Join Date
    Jan 2006
    Posts
    32
    well spotted, i hadn't noticed and nor did borland.

    the new line at the begining is just to drop it down so when it outputs in dos its not right at the top of the screen. other languages I like to put it at the end. I agree it causes less problems there, but not too much of a problem for this program.
    I'm a noob when it comes to C and I have to use Borland C++ 3.1 because my university won't move on.

    However I do use Turbo C++ 3.1 for windows. Makes life abit less stressful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a couple of questions about System.String
    By Elkvis in forum C# Programming
    Replies: 5
    Last Post: 02-17-2009, 01:48 PM
  2. A couple of Basic questions
    By ozumsafa in forum C Programming
    Replies: 8
    Last Post: 09-26-2007, 04:06 PM
  3. Couple of Questions
    By toonlover in forum Windows Programming
    Replies: 10
    Last Post: 07-14-2006, 01:04 AM
  4. New to C++/ Couple of questions...
    By danielthomas3 in forum C++ Programming
    Replies: 13
    Last Post: 04-14-2002, 03:46 PM
  5. couple questions for newb
    By bluehead in forum C++ Programming
    Replies: 10
    Last Post: 11-24-2001, 02:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21