Thread: n00b question.... dat file related

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    12

    n00b question.... dat file related

    I am new to the world of C and C++, now this may sound like a stupid quesiton, but this is the same way i learned visual basic, but anyways i wanna practice reading and writing to a dat file.

    now right now i have a dat file called people.dat and in that dat file i have 5 names

    Tom Cruise
    Lindsay Lohan
    Brad Pitt
    Britney Spears
    Jack Johnson

    now i want my practice application to read the dat file and display the list as is, then i want it to display reversed

    Jack Johnson
    Britney Spears
    Brad Pitt
    Lindsay Lohan
    Top Cruise

    i then want to write the reversed list to another dat file called reversed.dat...
    I am able to read the dat file and display in the order it is in in the dat file, but to display it in reverse and write it reversed is where i get lost.

    I know this is soo simple, but i wanna learn C/C++, there are too many vb programmers

    thanks guys/gals

  2. #2
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    There are a lot of C/C++ programmers too!

    Make a stab at the program and post back here with your problems. You will get MUCH better help that way!

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  3. #3
    ---
    Join Date
    May 2004
    Posts
    1,379
    Well don't know what your code looks like but lets say you have an array of characters that you read from the file...
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void){
      char str[14] = "Thisisastring";
      int i,len = strlen(str);
      
      for(i=len-1;i>-1;i--){
        printf("%c",str[i]);
      }  
      
      return 0;
    }
    You just need to loop through the string backwards displayin each character one at a time instead of using printf("%s",str) to display the whole string

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    36
    In this case, he wants to read strings, store them, and then print them in reversed order, or read the whole file into a buffer, change all the newlines to nul chars, then scan from the back of the buffer, printing each string as a line, or ....

  5. #5
    I typecast anyway.
    Join Date
    Jun 2005
    Posts
    25

    Smile Try this for starters.

    Quote Originally Posted by sand_man
    You just need to loop through the string backwards displayin each character one at a time instead of using printf("%s",str) to display the whole string
    No, no, no... that would print the characters backwards, he wants to print the LINES backwards with each string intact.

    Something like this should do it:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main () {
    
    	// Declare all variables at the beginning.
    	int i;
    	FILE *filePointer;
    	char stringPointers[5][100];
    
    	// Open file in read text (rt) mode.
    	filePointer = fopen ("myfile.dat", "rt");
    	if (filePointer == NULL) {
    		printf ("Error opening file: %s\n", "myfile.dat");
    		exit (1);  // Exiting without 0 means there was a problem.
    	}
    
    	// Read 5 lines (up to 99 chars each) and then close the file.
    	for (i = 0; i < 5; i++) {
    		fgets (stringPointers[i], 100, filePointer);
    	}
    	fclose (filePointer);
    
    	// Print in normal order.  Linefeeds should be in each string from fgets.
    	for (i = 0; i < 5; i++) {
    		printf ("%s", stringPointers[i]);
    	}
    
    	// Print a newline character to separate the outputs.
    	printf ("\n");
    
    	// Print in reverse order.
    	for (i = 4; i >= 0; i--) {
    		printf ("%s", stringPointers[i]);
    	}
    
    	return 0;
    }
    Or with dynamic allocation:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <malloc.h>
    
    int main () {
    
    	// Declare all variables at the beginning.
    	int i;
    	FILE *filePointer;
    	char **stringPointers;
    
    	// Allocate the array of 5 strings.
    	// This is a pointer to pointers (works as char[][]).
    	stringPointers = (char **) malloc (5 * sizeof (char *));
    
    	// Allocate each string (up to 99 chars in length each).
    	// This is a pointer to characters (works as char[]).
    	for (i = 0; i < 5; i++) {
    		stringPointers[i] = (char *) malloc (100);
    	}
    
    	// Open file in read text (rt) mode.
    	filePointer = fopen ("myfile.dat", "rt");
    	if (filePointer == NULL) {
    		printf ("Error opening file: %s\n", "myfile.dat");
    		exit (1);  // Exiting without 0 means there was a problem.
    	}
    
    	// Read 5 lines (up to 99 chars each) and then close the file.
    	for (i = 0; i < 5; i++) {
    		fgets (stringPointers[i], 100, filePointer);
    	}
    	fclose (filePointer);
    
    	// Print in normal order.  Linefeeds should be in each string from fgets.
    	for (i = 0; i < 5; i++) {
    		printf ("%s", stringPointers[i]);
    	}
    
    	// Print a newline character to separate the outputs.
    	printf ("\n");
    
    	// Print in reverse order.
    	for (i = 4; i >= 0; i--) {
    		printf ("%s", stringPointers[i]);
    	}
    
    	// Clean up memory that was allocated with malloc (good habit).
    	for (i = 0; i < 5; i++) {
    		free (stringPointers[i]);
    	}
    	free (stringPointers);
    
    	return 0;
    }
    Last edited by Raptor007; 06-23-2005 at 09:20 PM.

  6. #6
    ---
    Join Date
    May 2004
    Posts
    1,379
    No, no, no...
    Well thats what I get for not reading the original post properly

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    12
    thanks for the help guys, i'm workin with all you gave me, and i'm trying to make it work but these arrays i have to do some research on.. i'll get it workin soon..

  8. #8
    I typecast anyway.
    Join Date
    Jun 2005
    Posts
    25
    Something you might not be used to... all strings in C are arrays of characters, with a null terminator character telling it when the string ends.

    Therefore this:
    Code:
    char string[100];
    ...defines a string of maximum length 99, because it needs space to put a '\0' after the used characters to tell it the string has ended.

    Hope this helps!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Replies: 15
    Last Post: 10-31-2005, 08:29 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM