LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-01-2011, 05:47 PM   #1
abaddon4180
LQ Newbie
 
Registered: Dec 2011
Posts: 7

Rep: Reputation: Disabled
Trying to take name listings out of file and put into array (complete novice)


Like the title says I am trying to read in a file from the command line and then take the data from that, in the form of,

First Last
First Last
First Last
etc ...

And put it into an array of strings formatted as such,

Last, First
Last, First
Last, First

But I really have no idea where to start. Any help for someone just getting into programming would be appreciated.
 
Old 12-01-2011, 06:16 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability.

You may have to explain your requirements in more detail. Are you talking about scripting, some specific programming language, or does it matter which tool you use? And do you only want to reformat each line into "last, first", or do you need the values stored in arrays for other purposes as well? Are you even sure that an array is what you need to use?

The best techniques to use depend a lot on exactly what you're trying to accomplish.

For general bash scripting help, start with these links.

http://mywiki.wooledge.org/BashGuide
http://www.linuxcommand.org/index.php
http://mywiki.wooledge.org/BashFAQ
http://mywiki.wooledge.org/BashPitfalls

And see this page for a rundown on bash's string manipulation features:
http://mywiki.wooledge.org/BashFAQ/100

Last edited by David the H.; 12-01-2011 at 06:25 PM. Reason: minor addition & rewording
 
Old 12-01-2011, 06:32 PM   #3
abaddon4180
LQ Newbie
 
Registered: Dec 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
I need to be able to do some other things once I have the array; sort and search; but I think I can figure that out once I can actually get the array. I just don't know how to go about getting filling the array from the file.
 
Old 12-01-2011, 06:42 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Again, what language? And again, please provide us with some more details on what you want to do. How about showing us an actual example of the input, and how you want the data to be stored/manipulated?

From what you've said so far, it sounds to me like you might need some kind of multi-dimensional array to keep track of each separate entry as well as the individual data fields for them. bash can't do that directly, but it might be possible with linked associative arrays. Other languages might be more suited to the task.
 
Old 12-01-2011, 06:50 PM   #5
abaddon4180
LQ Newbie
 
Registered: Dec 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
Sorry about that. The language is just C and the input file would look something like,

names.txt

John Smith
Jane Doe
Jane Smith
John Doe
Jake Doe
Jake Smith

While the array has to look like,

Smith, John
Doe, Jane
Smith, Jane
etc ...

I think what I would have to do is individually read the characters from the file and copy them into the row of the array until I reach a new line and then go to the next index and do so again until I am at the end of the file. I have no idea how to do this, though. Once I have that array I think I could just use strcmp and strcpy to search and sort it, which is all I need to do.
 
Old 12-01-2011, 06:58 PM   #6
abaddon4180
LQ Newbie
 
Registered: Dec 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
I think this might work but I am not sure, especially when it comes to pointers and such which really confuse me
Code:
int main(int argc, char* argv[])
{
   char list[NAMES][LEN];
   
   if (argc != 2)
   {
      printf("\nWrong amount of command line arguments\n");
      return 0;
   }   
   else
      getArray(&argv[1], list);
}

void getArray(char* argv[], char list[][LEN])
{
   int i;
   FILE* fpIn;
   int cInput;
   
   fpIn = fopen(argv[1], "r");

      for (i = 0; i < NAMES; i++)
      {
      while((cInput = fgetc(fpIn)) != 10)
        * list[i] = putchar(cInput);
      }
   fclose(fpIn);
}
 
Old 12-01-2011, 07:32 PM   #7
abaddon4180
LQ Newbie
 
Registered: Dec 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
Just spent the last half hour typing this up and I know there are a lot of problems.

Code:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#define LEN 50
#define NAMES 10

//function declarions

bool menu(char list[][]);
void printArray(char list[][]);
void sortArray(char list[][]);
void searchArray(char list[][]);

int main(int argc, char* argv[])
{
   char list[NAMES][LEN];
   FILE* fpIn;
   int i;
   int cInput;
   
   if (argc != 2)
      printf("\nWrong amount of command line arguments\n");
   else if (!(fpIn = fopen(argv[1], "r")))
      printf("\nFile can't open");
   else
   {   
      for (i = 0; i < NAMES; i++)
      {
      while((cInput = fgetc(fpIn)) != 10)
         *list[i] = cInput;
      }
      menu(list);
   }
   
   fclose(fpIn);

   return 0;
}

bool menu(char list[][LEN])
{
   int userInput;
   bool endMenu = true;

   printf("\nPlease enter one of the following options: ");
   printf("\n   1: Sort in ascending ASCII order by last name");
   printf("\n   2: Search for a particular name");
   printf("\n   3: Print the collection of names");
   printf("\n   4: Quit\n\n");

   scanf("%d", userInput);
   if (userInput == 1)
   {
      sortArray(list);
      printArray(list);
      menu(list);
   }
   else if (userInput == 2)
   {
      searchArray(list);
      menu(list);      
   }
   else if (userInput == 3)
   {
      printArray(list);
      menu(list);
   }
   else if (userInput == 4)
      endMenu = false;
   else
      menu(list);

   return endMenu;
}

void printArray(char list[][LEN]) 
{
   int i;
 
   for (i = 0; i < NAMES; i++)
      printf("\n%s", list[i]); 
}

void searchArray(char list[][LEN])
{
   int i;
   char name[LEN];
   int result = -1;

   printf("\nPlease enter a name to search for: ");
   scanf("%s", name);

   for (i = 0; i < NAMES && result == -1; i++)
      if(strcmp(list[i], name) == 0)
         result = i;
   if(result == -1)
      printf("\nUnable to find name");
   else
      printf("\nName found at position %d in array", i);            
}

void sortArray(char list[][LEN])
{
   char temp[LEN];
   int  i;

   for (i = 0; i < NAMES; i++)
   {
      if(strcmp(list[i], list[i+1]) > 0)
      {
         strcpy(temp, list[i]);
         strcpy(list[i], list[i+1]);
         strcpy(list[i+1], temp);
      }
   } 
}
1. Every time I enter an integer when prompted I get a segmentation fault
2. When I run it without a valid file name in the command prompt I get a segmentation fault
3. I am sure I didn't fill the array correctly, if I filled it at all

Last edited by abaddon4180; 12-01-2011 at 07:57 PM.
 
Old 12-02-2011, 04:45 AM   #8
abaddon4180
LQ Newbie
 
Registered: Dec 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
Okay. I think I have most of the coding done, besides some formatting of the names, but every time I get to the menu and enter an integer I still get a segmentation fault. I know this is probably something stupid that I am doing, I just don't know what.
Code:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#define LEN 50
#define NAMES 10

//function declarions

bool menu(char list[][]);
void printArray(char list[][]);
void sortArray(char list[][]);
void searchArray(char list[][]);

int main(int argc, char* argv[])
{
   char list[NAMES][LEN];
   char name[50];
   FILE* fpIn;
   int i;
   int cInput;
   
   if (argc != 2)
      printf("\nWrong amount of command line arguments\n\n");
   else if (!(fpIn = fopen(argv[1], "r")))
      printf("\nFile can't open\n\n");
   else
   {
      while (!feof(fpIn))
      {         
         fscanf(fpIn, "%s", name);
         strcpy(list[i], name);
         i++;
      }         
      fclose(fpIn);

      menu(list);
   }
     
   return 0;
}

bool menu(char list[][LEN])
{
   int userInput;
   bool endMenu = true;

   printf("\nPlease enter one of the following options: ");
   printf("\n   1: Sort in ascending ASCII order by last name");
   printf("\n   2: Search for a particular name");
   printf("\n   3: Print the collection of names");
   printf("\n   4: Quit\n\n");

   scanf("%d", userInput);
   switch (userInput)
   {
   case 1: sortArray(list);
           printArray(list);
           break;
   case 2: searchArray(list);
           menu(list);
           break;
   case 3: printArray(list);
           menu(list);
           break;
   case 4: endMenu = false;
   default: menu(list);
   }

   return endMenu;
}

void printArray(char list[][LEN]) 
{
   int i;
 
   for (i = 0; i < NAMES; i++)
      printf("\n%s", list[i]); 
}

void searchArray(char list[][LEN])
{
   int i;
   char name[LEN];
   int result = -1;

   printf("\nPlease enter a name to search for: ");
   scanf("%s", name);

   for (i = 0; i < NAMES && result == -1; i++)
      if(strcmp(list[i], name) == 0)
         result = i;
   if(result == -1)
      printf("\nUnable to find name");
   else
      printf("\nName found at position %d in array", i);            
}

void sortArray(char list[][LEN])
{
   char temp[LEN];
   int  i;

   for (i = 0; i < NAMES; i++)
   {
      if(strcmp(list[i], list[i+1]) > 0)
      {
         strcpy(temp, list[i]);
         strcpy(list[i], list[i+1]);
         strcpy(list[i+1], temp);
      }
   } 
}
 
Old 12-02-2011, 04:58 AM   #9
abaddon4180
LQ Newbie
 
Registered: Dec 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
Never mind. Just realized I was missing the '&' in the scanf in the menu function. Like I said, I am new to programming.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
complete novice needs help with pidgin installation please clair37 Linux - Newbie 21 10-04-2010 11:55 PM
Need tutorial or how-to hard copy for Linux systems; complete novice. barbaraharsanyi Linux - Newbie 5 10-05-2008 09:28 PM
[bash] Put words from file to array mispunt Programming 4 11-04-2004 10:53 AM
Query: Programming for the complete novice Calum Programming 21 04-20-2002 03:46 AM
Excuse a Complete Novice?? fhbooker Linux - General 5 04-14-2002 07:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration