I am trying to input a text file with twenty unsorted names in it into an array and then perform a bubblesort and output to a new file. The filename for the txt file is names.txt
Here is what I have so far....not much...I would appreciate some help the right direction. I have read quite a bit on the subject but it is not quite fitting together in my head yet.

#include<stdio.h>
#include<stdlib.h>

int main (void)
{
    
  FILE *fptr;
  
  
  fptr1=fopen("names.txt", "r");

Open the file
Store the contents of the file, one line at a time into an array- Possibly an array of character pointers
Sort the character pointer array

I am trying to input a text file with twenty unsorted names in it into an array and then perform a bubblesort and output to a new file. The filename for the txt file is names.txt
Here is what I have so far....not much...I would appreciate some help the right direction. I have read quite a bit on the subject but it is not quite fitting together in my head yet.

#include<stdio.h>
#include<stdlib.h>

int main (void)
{
    
  FILE *fptr;
  
  
  fptr1=fopen("names.txt", "r");

The next step - did fopen return a valid FILE pointer...

The next step - did fopen return a valid FILE pointer...

I am still trying to input my text file into an array
Here is the code I have now..is this any closer?...I really need some input...I am very new at this.Thanks!

#include<stdio.h>
#include<stdlib.h>

int main (void)
{
  int bytes_read	
  int nbytes =80
  FILE *cfptr;
  
  
  fptr1=fopen("names.txt", "r");
  if (*cfptr=fopen(names.txt, "r"))==NULL) {
	  printf("File can not be opened.")
  }				/*end if*/

my_string = (char *) calloc (nbytes + 1);
  bytes_read = getline (&names, &nbytes, stdin);

Thanks to all for your help thus far. I am learning! I now have the text file I created reading into a char array and need to sort it using bubble sort and read into an output file. I have read some about this...but need some direction and help please. If I have to move my data into an array of pointers to do this I don't understand how to do that. Here is what I have so far.

#include <stdio.h>

int main() {
  char c[12];  /* declare a char array */
  FILE *file;  /* declare a FILE pointer  */

  file = fopen("names.txt", "r"); 
  /* open a text file for reading */

  if(file==NULL) {
    printf("Error: can't open file.\n");
    /* fclose(file); DON'T PASS A NULL POINTER TO fclose !! */
    return 1;
  }
  else {
    printf("File opened successfully. Contents:\n\n");
    
    while(fgets(c, 12, file)!=NULL) { 
      /* keep looping until NULL pointer... */
      printf("String: %s", c);        
      /* print the file one line at a time  */
    }

    printf("\n\nNow closing file...\n");
    fclose(file);
    return 0;
  }
}

http://ee.hawaii.edu/~tep/EE160/Book/chap9/section2.1.4.html


This should help.

Thank you. This information helps my understanding quite a bit. However, I do have to use bubble sort for this particular program. Would someone be willing to help me use that method to sort my array?. I do not quite understand how to do it. Is it necessary for me to create an array of pointers to use that sort method also? I know these questions show my lack of experience but I have to learn somehow.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.