Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you put lines from a text file into an Array?

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
0
Here is the code I have so far.


#include <stdio.h>
#include <string.h>

#define MAXLIN 150
int main()
{
FILE *f1;
char string[MAXLIN];
char got[MAXLIN];
static char mname[MAXLIN];
int fi = 0;
f1=fopen(&quot;all.m3u&quot;, &quot;r&quot;);
while ( fgets( string, MAXLIN, f1) != NULL)

if (string[0] != '#')
{
strcpy(got,strrchr(string,'\\'));
strcpy(mname,(&got[1]));

printf(&quot;%s&quot;,mname);
fi++;
}
return(0);
}


What I'm trying to do is put the mname variable into an array like umame[fi][0], so that I can then move back and forth through it in a loop,

Here is some of the all.m3u file to show structure:
#EXTM3U
#EXTINF:247,2 Unlimited - Twilight Zone
\\csgld420\MUSIC\My Music\2 Unlimited - Twilight Zone.mp3
#EXTINF:355,A-Ha - Take On Me (Tecno Mix)
\\csgld420\MUSIC\My Music\A-Ha - Take On Me (Tecno Mix).mp3
#EXTINF:216,Abc - How To Be A Millionaire
\\csgld420\MUSIC\My Music\ABC - How To Be A Millionaire.mp3
#EXTINF:209,Abc - Look Of Love
\\csgld420\MUSIC\My Music\ABC - Look of Love.mp3
#EXTINF:265,Abc - When Smokey Sings
\\csgld420\MUSIC\My Music\ABC - When Smokey Sings.mp3
#EXTINF:343,AC\DC - For Those About To Rock
\\csgld420\MUSIC\My Music\ACDC - For Those About To Rock.mp3
#EXTINF:211,AC\DC - You Shook Me All Night Long
\\csgld420\MUSIC\My Music\ACDC - You Shook Me All Night Long.mp3
#EXTINF:211,Ace Of Base - All That She Wants
\\csgld420\MUSIC\My Music\Ace Of Base - All That She Wants.mp3

Now my code gives me almost what I want, but I can't seem to put it into any array, I get various errors.

Here is an example of the output from the code I have so far:

2 Unlimited - Twilight Zone.mp3
A-Ha - Take On Me (Tecno Mix).mp3
ABC - How To Be A Millionaire.mp3
ABC - Look of Love.mp3
ABC - When Smokey Sings.mp3
ACDC - For Those About To Rock.mp3
ACDC - You Shook Me All Night Long.mp3
Ace Of Base - All That She Wants.mp3

Which is fine, but I need to wack the CR and the &quot;.mp3&quot; off the end still.

I want to be able to call the array like this in the end.

printf(&quot;%s&quot;,newname[fi]); /* or newname[32] */
load_amp(newname[fi],0);

I have tried:
strcpy(newname[fi],mname); but it errors and GPF when ran
newname[fi] = mname; same as above

I need the info to look like this in the end:
newname[0] = 2 Unlimited - Twilight Zone
newname[1] = A-Ha - Take On Me (Tecno Mix)
newname[2] = ABC - How To Be A Millionaire
newname[3] = ABC - Look of Love
newname[4] = ABC - When Smokey Sings
newname[5] = ACDC - For Those About To Rock
newname[6] = ACDC - You Shook Me All Night Long
newname[7] = Ace Of Base - All That She Wants

or newname[0][0] = 2 Unlimited - Twilight Zone

I'm not sure,

Please tell me what I'm doing wrong.

If its all wrong, please tell me that as well.
Thank you in advance for any help you can give me.
Scott

 
you can eliminate the .mpg<cr> by searching for the .mpg using strstr and then replacing the . with a NULL.

your strcpy(newname[fi],mname); statement is looking for two pointers strcpy(const char *, const char *). newname[fi] is the value of newname at location fi. You actually want the address of that location to be saved in the pointer memory space so try this strcpy(&newname[fi],mname); This should save the address of newname at that memory location fi in the pointer memory space.

You mentioned a two dimensional array. I am not sure what you want here but try a structure.

Hope this helps
 
#include <string>
#include <fstream>
#include <vector>

using namespace std;

int main()
{
string temp;
vector<string> string_vector;
ifstream infile(&quot;all.m3u&quot;);
while(infile){
getline(infile, temp);
string_vector.push(temp);
}
}

This will fill the vector with the lines from the file.
 
// filearray.c by C Williams
// for educational purposes only
// this header function will read a file into
// an array and return a pointer to the array

/*
be sure to have these in your main project
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

*/

#define MAXLINES 99999

void clrbuf(char string[]);
char **file_array(char file_name[]);


char **file_array(char file_name[])
{

FILE *inFile;
char *str_array[MAXLINES];
char **p_str_array;
char string[255];
char *p_string;
int arr_cnt;

arr_cnt = 0;

inFile = fopen(file_name, &quot;r&quot;);
if (! inFile)
{
printf(&quot;\nError reported by file_array():\n%s not found!\n&quot;, file_name);
exit (1);
}

while (! feof(inFile))
{
clrbuf(string);
fgets(string,255,inFile);
p_string = string;
str_array[arr_cnt] = strdup(p_string);
arr_cnt++;
}

fclose(inFile);

p_str_array = str_array;

// note: remember to free array later!!!
return(p_str_array);

}

void clrbuf(char string[])
{
// nifty little function to blank out our reusable variables
int x;
int y;
y = strlen(string);
for (x = 0; x == y; x++)
string[x] = '\0';
return;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top