如何将文件中"v"行之后的所有数字存入verts数组
Solution to Extract All Numbers After "v" into verts[] Array
Got it, let's work through this problem together. The goal is to pull every numeric value that comes after a line starting with "v" (I’m assuming this is similar to OBJ file syntax, but the approach works regardless) and stuff them all into your verts array—even if the number of values per "v" line varies.
Step-by-Step Approach
Here's the core logic we'll follow:
- Iterate through each line of your file (no need to load the whole file at once, which is better for large files)
- Spot lines that start with our target "v" prefix (we’ll make sure we’re matching the exact prefix, not just a word containing "v")
- For each matching line, extract every numeric value that comes after "v"
- Append each of those numbers to the
vertsarray
Code Example (Python)
Python is great for this kind of text processing because it’s concise and handles variable-length inputs easily:
verts = [] # Replace "your_file.txt" with your actual file path with open("your_file.txt", "r") as input_file: for line in input_file: # Clean up the line to handle extra spaces/newlines cleaned_line = line.strip() # Skip empty lines if not cleaned_line: continue # Split the line into parts to check the first element line_parts = cleaned_line.split() # Make sure the first element is exactly "v" if line_parts[0] == "v": # Loop through all remaining parts and convert to numbers for num_str in line_parts[1:]: try: # Use int() instead of float() if you need integer values num = float(num_str) verts.append(num) except ValueError: # Optional: handle cases where a value isn't a valid number print(f"Skipping invalid numeric value: {num_str}") # Verify the result print("Extracted vertices:", verts)
Code Example (C++)
If you're working in a compiled language like C++, here's a robust implementation:
#include <iostream> #include <fstream> #include <vector> #include <string> #include <sstream> #include <limits> using namespace std; int main() { vector<double> verts; ifstream input_file("your_file.txt"); string line; if (!input_file.is_open()) { cerr << "Error: Could not open the file." << endl; return 1; } while (getline(input_file, line)) { istringstream line_stream(line); string prefix; // Read the first word of the line line_stream >> prefix; if (prefix == "v") { double num; // Keep reading numbers until the end of the line while (line_stream >> num) { verts.push_back(num); } // Reset stream state in case we hit non-numeric data line_stream.clear(); line_stream.ignore(numeric_limits<streamsize>::max(), '\n'); } } input_file.close(); // Print the extracted values cout << "Extracted vertices: "; for (double val : verts) { cout << val << " "; } cout << endl; return 0; }
Key Notes
- Flexible Value Count: Both examples handle any number of values after "v"—whether it's 2, 3, or more per line.
- Error Handling: The try-except (Python) and stream reset (C++) prevent crashes if your file has unexpected non-numeric data after "v".
- Exact Prefix Match: Using
line_parts[0] == "v"(Python) or reading the first word (C++) ensures we don't accidentally match lines like "vertex 1 2 3"—adjust this if your actual prefix is slightly different (e.g., "v " with a space).
内容的提问来源于stack exchange,提问作者Im Bulletproof
相关产品推荐
相关产品推荐

